由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - protected/private inheritance
相关主题
ask a C++ inheritance questionC++: protect dtor
考考大家,其实也不只是C++,C#/Java也一样default Specifier是什么?
two c++ interview questions! (转载)Question on C++ Access Control (protected)
protected class member in C++C++子类中调用父类指针的问题
C++ Q01: private inheritance.C++ virtual function 问题
C++ Primer 上关于inheritance protected member 的一段话g++编译了不该过的C++代码
问一个inheritance的初级问题C++做题,麻烦师傅们再看看。
public and protected member in private inherit谷歌技术大汇总
相关话题的讨论汇总
话题: cout话题: void话题: private话题: public话题: endl
进入Programming版参与讨论
1 (共1页)
g*********s
发帖数: 1782
1
统计一下,有多少人不查书就知道准确含义?有多少人平时用这两种继承?
另外明明是public inheritance最常见,为何要把default设成private inheritance?
g*********s
发帖数: 1782
2
Just did a quick test. So what's the diff b/w private inheritance and
protected inheritance here?
#include
using namespace std;
class B {
public:
void f() { cout << "B::f()\n"; }
virtual void g() { cout << "B::g()\n"; }
int B_public;
protected:
int B_protected;
private:
int B_private;
};
class D_pub: public B {
public:
void f() { cout << "D_pub::f()\n"; }
virtual void g() { cout << "D_pub::g()\n"; }
void h() { cout << B_protected << endl; }
//void j() { cout << B_private << endl; }
void k() { cout << B_public << endl; }
private:
int D_dat;
};
class D_pro: protected B {
public:
void f() { cout << "D_pro::f()\n"; }
virtual void g() { cout << "D_pro::g()\n"; }
void h() { cout << B_protected << endl; }
//void j() { cout << B_private << endl; }
void k() { cout << B_public << endl; }
private:
int D_dat;
};
class D_pri: private B {
public:
void f() { cout << "D_pri::f()\n"; }
virtual void g() { cout << "D_pri::g()\n"; }
void h() { cout << B_protected << endl; }
//void j() { cout << B_private << endl; }
void k() { cout << B_public << endl; }
private:
int D_dat;
};

【在 g*********s 的大作中提到】
: 统计一下,有多少人不查书就知道准确含义?有多少人平时用这两种继承?
: 另外明明是public inheritance最常见,为何要把default设成private inheritance?

g*********s
发帖数: 1782
3
class D_pro2: public D_pro {
public:
virtual void g() { cout << "D_pro2::g()\n"; }
void h() { cout << B_protected << endl; }
//void j() { cout << B_private << endl; }
void k() { cout << B_public << endl; }
};
class D_pri2: public D_pri {
virtual void g() { cout << "D_pri2::g()\n"; }
void h() { cout << B_protected << endl; }
//void j() { cout << B_private << endl; }
void k() { cout << B_public << endl; }
};
access_control.cpp: In member function ‘void D_pri2::h()’:
access_control.cpp:11: error: ‘int B::B_protected’ is protected
access_control.cpp:59: error: within this context
access_control.cpp: In member function ‘void D_pri2::k()’:
access_control.cpp:9: error: ‘int B::B_public’ is inaccessible
access_control.cpp:61: error: within this context
B_protected and B_public are now both private in D_pri, and thus D_pri2
can't access it. But why they have the different error messages?

【在 g*********s 的大作中提到】
: Just did a quick test. So what's the diff b/w private inheritance and
: protected inheritance here?
: #include
: using namespace std;
: class B {
: public:
: void f() { cout << "B::f()\n"; }
: virtual void g() { cout << "B::g()\n"; }
: int B_public;
: protected:

g*********s
发帖数: 1782
4
i figure out the following formula to determine the access level
transition. can daniu confirm its correctness?
enum ACC_LEV {
PRIVATE,
PROTECTED,
PUBLIC
};
derived.dat.access_level = min(base.data.access_level,
inheritance.access_level);
so if B::dat is private, and base class B is public inherited by derived
class D, then B::dat in D is still private, and thus inaccessible by D
and its descents.
however, if dat is public while B is private inherited by D, then B::dat
is now private in D and thus inaccessible by D's descents. but it's
still OK for D to access it, as now B::dat is like a private member of
D.

【在 g*********s 的大作中提到】
: class D_pro2: public D_pro {
: public:
: virtual void g() { cout << "D_pro2::g()\n"; }
: void h() { cout << B_protected << endl; }
: //void j() { cout << B_private << endl; }
: void k() { cout << B_public << endl; }
: };
: class D_pri2: public D_pri {
: virtual void g() { cout << "D_pri2::g()\n"; }
: void h() { cout << B_protected << endl; }

e****d
发帖数: 895
5
Struct's default is public inheritance.

【在 g*********s 的大作中提到】
: 统计一下,有多少人不查书就知道准确含义?有多少人平时用这两种继承?
: 另外明明是public inheritance最常见,为何要把default设成private inheritance?

a****o
发帖数: 686
6
这玩意有什么可讨论的?不就是子类所含有的成员授予外界访问的权限的最少限制的归一化么?C++是要用class把struct彻底抛弃掉.设置成private的默认也是出于encapsulation的语言设计理念,这是偶的一孔之见.
private inheritance 类似于 composition. 虽然的哦失去了 is-a 的类型, 但是不同是, private inheritance 允许自身的成员将子类指针转变为基类指针, composition则不可以,而且 private inheritance允许访问基类的 protected. 私有继承可以重载虚函数,但是不能有多个基类的实体.如果有多个,用composition.能用组合就组合,不行了再上private.
我觉得版上大牛们都是不需要查书就能知道的.

【在 g*********s 的大作中提到】
: 统计一下,有多少人不查书就知道准确含义?有多少人平时用这两种继承?
: 另外明明是public inheritance最常见,为何要把default设成private inheritance?

1 (共1页)
进入Programming版参与讨论
相关主题
谷歌技术大汇总C++ Q01: private inheritance.
版上有CS 学位的 有多少能写个最简单的compiler?C++ Primer 上关于inheritance protected member 的一段话
Re: beg: 难题:也是关于内存的问题问一个inheritance的初级问题
Global(static) variable initialization questionpublic and protected member in private inherit
ask a C++ inheritance questionC++: protect dtor
考考大家,其实也不只是C++,C#/Java也一样default Specifier是什么?
two c++ interview questions! (转载)Question on C++ Access Control (protected)
protected class member in C++C++子类中调用父类指针的问题
相关话题的讨论汇总
话题: cout话题: void话题: private话题: public话题: endl