#include<iostream>
using namespace std;
class uvw;
class abc{
private:
int privateMember;
protected:
int protMember;
public:
int publicMember;
};
class def : private abc{
public:
void dummy_fn();
};
class uvw: public def{
};
void def::dummy_fn()
{
abc x;
def y;
uvw z;
cout << z.protMember << endl; // This can be accessed and doesn't give a compile-error
}
据我所知,在
def
私有继承 abc
后,protMember
和 publicMember
在 def
中变为私有成员。因此,现在当 uvw
从 def
继承时,它不应该有任何数据成员。但是我们可以通过 dummy_fn()
奇怪地访问到 z.protMember
,而 z
在第一次出现时根本就没有定义变量 protMember
。我哪里错了吗?
dummy_fn
是def
的成员函数有关。但这只是猜测。 - Kiril Kirov