且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用指向派生类中基类的指针访问受基类保护的成员

更新时间:2023-02-14 23:28:34

是的,这是预期的行为。可以在派生类中访问基类的受保护的成员,但是仅通过派生类(或当前派生类的进一步派生类)(包括 this )类型的对象。这意味着您无法通过指向基类的指针访问受保护的成员。

Yes, this is expected behavior. Protected members of base class can be accessed in the derived class, but only via an object of a type that is the derived class (or the further derived class of current derived class) (including this). That means you can't access protected members via a pointer to base class.


只能访问基类的受保护成员

A protected member of a class Base can only be accessed

1)...

2)由Base派生的任何类的成员,但仅当
对从Base(包括
this)派生的类型的对象进行操作

2) by the members of any class derived from Base, but only when operating on an object of a type that is derived from Base (including this)