为什么此子类代码会导致运行时错误
Why does this subclass code cause a runtime error?
我有一个我使用X
扩展的基类A
。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;
}
P.S。之所以出现,是因为我的代码有不同的问题,但是我什至无法使这个玩具示例起作用,所以现在我让我感到好奇了....
这是上述代码的链接,该链接正在编译和无法运行:http://ideone.com/mbj1kg
它运行良好。IDEONE报告了一个"运行时错误",其退出代码为1,因为您从main
返回1
。通常认为非零返回代码是故障。
如果您评论return x.b_.value
行并将其替换为return 0
,则一切都很好。
您可能已经在其中放了一些std::cout
行以查看发生了什么,并查看程序有效!:D
相关文章:
- 为什么此代码存在运行时错误?
- 蝴蝶代码 88.合并排序数组 c++ 运行时错误
- 在Hackerearth上获取运行时错误(SIGSEGV),无法找出代码中的问题
- 为什么我在此代码中收到运行时错误
- 我的代码上的运行时错误 sigsegv
- 矢量的 c++ 代码中的运行时错误
- C++代码在 for 循环的条件下给出运行时错误,而如果它被具有相同意义的代码替换,则编译正确
- 我的代码中出现运行时错误的可能原因是什么?
- 我在代码中收到一个运行时错误,该错误如何通过修改最多一个元素来查找数组是否可以变得不减少
- 在计算机级别,应用程序以二进制方式运行,然后当发生运行时错误时,它如何呈现回错误代码
- .NET CORE 2 DLL引用带有C 代码运行时错误
- 为什么我的代码给出运行时错误超时
- 为什么我的程序在删除以下代码中的注释后会出现运行时错误?
- 薄板样条形状转换运行时错误 [使用代码 -1073741819 退出]
- C 代码的运行时错误(基于指针)
- 代码中的运行时错误,用于求解最小的偶数可整除
- 为什么我的代码导致运行时错误
- 为什么矢量上的这段代码会给出运行时错误
- 涉及向量的初学者代码,SEIGSEV运行时错误
- 为什么这段代码会产生运行时错误