【发布时间】:2018-08-10 03:45:44
【问题描述】:
为什么派生类 Derived_from_Private 是非法的? 我注意到成员函数引用了 Base,但为什么它不能引用 Base 类?
class Base {
public:
void pub_mem(); // public member
protected:
int prot_mem; // protected member
private:
char priv_mem; // private member
};
struct Pub_Derv : public Base {
// legal
void memfcn(Base &b) { b = *this; }
};
struct Priv_Derv : private Base {
// legal
void memfcn(Base &b) { b = *this; }
};
struct Prot_Derv : protected Base {
// legal
void memfcn(Base &b) { b = *this; }
};
struct Derived_from_Public : public Pub_Derv {
// legal
void memfcn(Base &b) { b = *this; }
};
struct Derived_from_Private : public Priv_Derv {
// illegal
void memfcn(Base &b) { b = *this; }
};
struct Derived_from_Protected : public Prot_Derv {
// legal
void memfcn(Base &b) { b = *this; }
};
【问题讨论】:
-
你对protected和private这两个关键字的理解是什么?
-
私有继承没有隐式转换到基类(你必须使用强制转换)
标签: c++ class c++11 inheritance