关于基类对派生类的访问
#include <iostream>
using namespace std;
class base
{
public:
base(const int&m,const int &n):x(m),y(n){}
base(const base& t){
x=t.x;
y=t.y;
z=t.z;
}
virtual void display(){
cout << x << ' ' << y << ' ' << z << endl;
}
virtual ~base(){};
protected:
int x;
int y;
private:
int z;
};
class derived:public base
{
public:
derived(const int&m,const int&n,const int&p):base(m,n),z(p){}
void display(){
cout << x << ' ' << y << ' ' << z << endl;
}
int z;
};
int main()
{
derived tb(5,7,8);
base ta(tb);
ta.display();
tb.display();
cout << tb.z;
system("pause");
}
为什么在派生类转换为基类中,基类的成员函数能对派生类的protected的成员访问.
却不能对public中的z成员访问(z无法初始化).