【发布时间】:2010-12-31 23:42:34
【问题描述】:
首先,下面的代码不能用 Visual c++ 工作,而是用流血工作
输出为 0 ,但 acc.对我来说应该是 1 ;谁能解释一下这个
#include<iostream>
using namespace std;
class shape
{
public:
virtual void print() const =0;
virtual double area() { return 0.0;}
};
class point : public shape
{
int x;
int y;
public :
point(int a=11, int b=11)
{
x=a;
shape *s;
s=this;
cout<<s->area();
y=b;
}
double area()const {return 1.0;}
void print() const
{
cout<<"\nPoint\n";
cout<<x<<"\t"<<y;
}
};
int main()
{
point p(1,2);
return 0;
}
【问题讨论】:
-
重复,参见 e。 G。 stackoverflow.com/questions/962132/…
-
我投票支持重新开放。这不是完全重复的。这个问题与 const/nonconst 覆盖有问题,并且调用没有发生在基类的构造函数中(与链接的“重复”相反)。声称任何构造函数问题的答案是完全错误的。如果您在 main 中执行
cout << ((shape*)&p)->area();,则会出现同样的问题
标签: c++ constructor virtual