class A {
public:
void foo() { cout << "Work";}
void bar() { this->foo(); }//new edit, works too!
};
class B {
private:
A *a; //never initialized
public:
A& getA() {
return *a;
}
};
void SomeFunction() {
B *b = new B();
B& bRef = *b;
bRef.getA().bar();//edited
delete b;
}
我在没有初始化“a”的情况下调用了 SomeFunction(),它仍然正确打印“Work”。我不明白为什么,它应该因分段错误而退出!