C++ Upcasting 基类指针指向派生类

请问为什么去掉标记出来的const之后输出就成了0 而不是0,2 谢谢
#include
using namespace std;
class Base{
protected:
int x;
public:
Base(int b=0): x(b) { }
virtual void display() const {cout << x << endl;}
};
class Derived: public Base{
int y;
public:
Derived(int d=0): y(d) { }
void display() _ const_ {cout << x << "," << y << endl;}
};
int main()
{
Derived d(2);
Base *p = &d;
p->display();
return 0;
}

呼啦一阵风
浏览 1005回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP