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?
|
|