为什么这个子类代码会导致运行时错误?

Rob*_*tin 2 c++ oop

我有一个基类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;
}
Run Code Online (Sandbox Code Playgroud)

PS这是因为我的代码遇到了不同的问题,但我甚至无法让这个玩具示例工作,所以现在我有这个让我好奇....

以下是上述代码的链接,该代码正在编译并且无法运行:http://ideone.com/mBJ1Kg

Lig*_*ica 7

它运行正常.因为您返回ideone报告一个"运行时错误"为1的退出代码1main.通常认为非零返回码是失败的.

如果你注释掉你的return x.b_.value线并替换它,return 0那么一切都很好.

你可以std::cout在那里放一些线来看看发生了什么,看看程序是否有效!:d