class BASE
{
public:
BASE(): aa(293) {}
virtual
void print() {cout<<"Hello\n";}
virtual
void
set() {aa =
103;}
int aa;
};
class Derived : private BASE //换成protected也一样
{
public:
//Derived(): BASE(103) {}
BASE* pr(Derived* d)
{
return d;
}
void
set(){aa=39;}
void print(){;cout<<"World\n"
<<
this->aa << endl;}
};
int main()
{
//BASE *p = new Derived;//为什么派生类的指针不能给基类
Derived dd;
BASE *p = dd.pr(new Derived); //这样就行,原先我理解。private继承,基类中的所有成员在派生类中为private访问
//编译器拒绝将 派生类的指针转换到基类。。是为了防止,用户调用在派生类中 已经是private基类的成员。。
//难道这不是派生类指针?
p->print();
cout << p->aa << endl;
p->set();
cout << p->aa;
}
|