由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - Question on C++ Access Control (protected)
相关主题
C++ templatec++ 得最基本问题
question about const referenceC++ namespace 弱问
大家要学习C++11啊, 我觉得C++11很多FEATURE,绝对不输JAVA.C++ 用户定义exception的标准用法是什么?
请教一个C++ typedef的问题不用头文件,如何调用函数?C++
C++必备书籍推荐请问如何把初始化一个const 的vector (or array) in a class?
C++ class cross reference problem一个C++ template的问题
用STL map的时候怎么自己定义大小比较的关系C++ STL map find does not work ???
C++ template question问个c++问题
相关话题的讨论汇总
话题: protected话题: question话题: const话题: c++话题: access
进入Programming版参与讨论
1 (共1页)
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

1 (共1页)
进入Programming版参与讨论
相关主题
问个c++问题C++必备书籍推荐
const_reverse_iterator和reverse_iterator有什么区别? (转载)C++ class cross reference problem
请教如何自己C++编程牛逼些用STL map的时候怎么自己定义大小比较的关系
c++ does not check const for extern variable?C++ template question
C++ templatec++ 得最基本问题
question about const referenceC++ namespace 弱问
大家要学习C++11啊, 我觉得C++11很多FEATURE,绝对不输JAVA.C++ 用户定义exception的标准用法是什么?
请教一个C++ typedef的问题不用头文件,如何调用函数?C++
相关话题的讨论汇总
话题: protected话题: question话题: const话题: c++话题: access