Redian新闻
>
Question on C++ Access Control (protected)
avatar
Question on C++ Access Control (protected)# Programming - 葵花宝典
vi
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!
avatar
c*t
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;

avatar
vi
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

avatar
c*t
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

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。