基类指针(or ref)可以指向 所有 的子类 (e.g., overriding),除了private 子类的所有子类 。意味着 private class 可用于隔断上下层的类的访问。
e.g., void memfcn (Base &b) { b = *this;} in class drv_from_Drv3
class Base
{
public:
double dBPublic = 0.1;
protected:
double dBProtect = 0.2;
private:
double dBPrivate = 0.3;
};
class Drv1 : public Base
{
public:
double dpubdrvPublic = 1.1;
void memfcn(Base &b) {b = *this;}
protected:
double dpubdrvProtect = 1.2;
private:
double dpubdrvPrivate = 1.3;
};
class Drv2 : protected Base
{
public:
double dprodrvPublic = 2.1;
void memfcn(Base &b) {b = *this;}
protected:
double dprodrvProtect = 2.2;
private:
double dprodrvPrivate = 2.3;
};
class Drv3 : private Base
{
public:
double dpridrvPublic = 2.1;
void memfcn(Base &b) {b = *this;}
protected:
double dpridrvProtect = 2.2;
private:
double dpridrvPrivate = 2.3;
};
class drv_from_Drv1 : public Drv1
{
public:
double dpubdrvPublic = 3.1;
void memfcn(Base &b) {b = *this;}
protected:
double dpubdrvProtect = 3.2;
private:
double dpubdrvPrivate = 3.3;
};
class drv_from_Drv2 : public Drv2
{
public:
double dpubdrvPublic = 4.1;
void memfcn(Base &b) {b = *this;}
protected:
double dpubdrvProtect = 4.2;
private:
double dpubdrvPrivate = 4.3;
};
class drv_from_Drv3 : public Drv3
{
public:
double dpubdrvPublic = 5.1;
void memfcn(Base &b) {b = *this;} //编译报错.
//原因: Drv3 is private, all the member that Drv3 inherit from Base are private.
protected:
double dpubdrvProtect = 5.2;
private:
double dpubdrvPrivate = 5.3;
};
没有评论:
发表评论