I have a base class A that I am extending with X. Inside of A there is another class, B. It seems like the virtual method is not being defined, but I don’t understand why?
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. This came up because I am having a different problem with my code, but I couldn’t even make this toy example work, so now I have this making me curious….
Here is a link to the above code, which is compiling and failing to run: http://ideone.com/mBJ1Kg
It runs fine. ideone reports a “runtime error” with an exit code of 1 because you return
1frommain. A non-zero return code is conventionally considered to be a failure.If you comment out your
return x.b_.valueline and replace it withreturn 0then everything’s fine.You could have put some
std::coutlines in there to see what’s going on, and see that the program works! 😀