//派生类对象初始化基类的引用
//引用是别名,但这个别名只能包含派生类对象中的由基类继承来的隐藏对象
#include <iostream>
using namespace std;
class B
{
public:
B()
{
cout<<"B"<<endl;
}
void fun()
{
cout<<"B::fun()"<<endl;
}
private:
int x;
};
class D : public B
{
public:
D()
{
cout<<"D"<<endl;
}
void fun()
{
cout<<"D::fun()"<<endl;
}
void show()
{
cout<<"D::show()"<<endl;
}
private:
int y;
};
void main()
{
D d;
B &p=d;
p.fun();
// p.show(); //错误,派生类对象初始化基类的引用不能用于派生类
}
<img src="https://img-blog.csdn.net/20150512213405630?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZG91ZG91d2ExMjM0/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" />
【C++】派生类对象初始化基类的引用
最新推荐文章于 2023-03-15 13:57:42 发布