基类指针指向其派生类#includeiostreamusing namespace stdclass X1{protected:int xpublic:X1(int xx
基类指针指向其派生类
#include<iostream>
using namespace std;
class X1
{
protected:
int x;
public:
X1(int xx){x=xx;}
virtual void output()
{ ; }
};
class Y1:protected X1
{
int y;
public:
Y1(int xx=0,int yy=0):X1(xx)
{
y=yy;
}
virtual void output()
{
cout<<"x="<<x<<"y="<<y<<endl;
}
};
void main()
{
X1 a(2);
Y1 b(3,4);
X1 * xp = & b; //-------这里报错了
}
这里如果派生类是protected方式继承的话? 其基类指针没法指向这个派生类的对象么?
求大神指点,说一下这类情况还有哪些? 指针指向派生来 指针 类
[解决办法]
你这个要用public继承才能用Y的对象给X的指针赋值
