vi 发帖数: 309 | 1 Although I vaguely know the differences between private and protected,
but I am not sure on exactly where should I use which. For an example,
STL list is declared as:
template >
class stack {
public:
bool empty() const;
size_type size() const;
value_type& top();
const value_type& top() const;
void push(const value_type& val);
void pop();
protected:
Container c;
};
Question: why 'protected' is used here?
Thanks! | c*****t 发帖数: 1879 | 2 In general, avoid protected data members as much as possible as it
can cause serious problems when doing inheritance and multiple
inheritances.
One situation is that due to deeply inherited class hierarchy, typically
in GUI framework, a child class declare a same named variable. So
mysterious bugs can occur.
A counter argument is that usually one should use composition (as seen
here in the stack implementation) rather than inheritance. The purpose
of declaring a protected variable is to allow
【在 vi 的大作中提到】 : Although I vaguely know the differences between private and protected, : but I am not sure on exactly where should I use which. For an example, : STL list is declared as: : template > : class stack { : public: : bool empty() const; : size_type size() const; : value_type& top(); : const value_type& top() const;
| vi 发帖数: 309 | 3
Thank you so much for the in depth explanation. There is such a long
way to go as I am just start learning; really admire your perspective
understandings. So in the original questions, as the original code is
copy and paste directly from the GNU STL implementation, why do they
use 'protected' here rather than 'private'?
I am reading Stroustrup's The C++ Programming Language. On page 405,
he said 'In particular, declaring data members protected is usually a
design error', and 'protected is a
【在 c*****t 的大作中提到】 : In general, avoid protected data members as much as possible as it : can cause serious problems when doing inheritance and multiple : inheritances. : One situation is that due to deeply inherited class hierarchy, typically : in GUI framework, a child class declare a same named variable. So : mysterious bugs can occur. : A counter argument is that usually one should use composition (as seen : here in the stack implementation) rather than inheritance. The purpose : of declaring a protected variable is to allow
| c*****t 发帖数: 1879 | 4 hint: all programmers are human beings :)
The most likely reason he/she declared it as protected was to allow people
to inspect the container for debugging purpose. The class clearly wasn't
meant to be extended since no methods were virtual, so it was no big deal.
【在 vi 的大作中提到】 : : Thank you so much for the in depth explanation. There is such a long : way to go as I am just start learning; really admire your perspective : understandings. So in the original questions, as the original code is : copy and paste directly from the GNU STL implementation, why do they : use 'protected' here rather than 'private'? : I am reading Stroustrup's The C++ Programming Language. On page 405, : he said 'In particular, declaring data members protected is usually a : design error', and 'protected is a
|
|