[英]Why does this subclass code cause a runtime error?
我有一个基类A
,我用X
扩展。 A
里面有另一个班, B
。 似乎虚拟方法没有定义,但我不明白为什么?
class A {
public:
class B {public: bool value;};
A() {}
B b_;
void DoStuff(B& b);
private:
virtual void DoStuffImpl(B& b) = 0;
};
class X : public A {
public:
X() {}
void Trigger();
private:
virtual void DoStuffImpl(B& b);
};
void A::DoStuff(B& b) {
DoStuffImpl(b);
}
void X::Trigger() {
DoStuff(b_);
}
void X::DoStuffImpl(B& b) {
b.value = true;
}
int main(){
X x;
x.Trigger();
return x.b_.value;
}
PS这是因为我的代码遇到了不同的问题,但我甚至无法让这个玩具示例工作,所以现在我有这个让我好奇....
以下是上述代码的链接,该代码正在编译并且无法运行: http : //ideone.com/mBJ1Kg
它运行正常。 ideone报告退出代码为1的“运行时错误”,因为您从main
返回1
。 通常认为非零返回码是失败的。
如果你注释掉你的return x.b_.value
行并用return 0
替换它,那么一切都很好 。
你可以在那里放一些std::cout
线来看看发生了什么,看看程序是否有效! :d
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.