好吧,所以我知道有很多多態線程在飛行,但我還沒有遇到這種情況。C++中的多態性
class Base {
public:
virtual void method1() {
cout << "BaseMethod1" << endl;
}
void method2() {
cout << "BaseMethod2" << endl;
}
};
class Derive: public Base {
public:
void method1() {
cout << "DeriveMethod1" << endl;
method2();
}
void method2() {
cout << "DeriveMethod2" << endl;
}
};
int main() {
Base* p = new Derive();
p->method1();
}
什麼讓我跳起來是派生類中的method1調用method2。那麼,由於基類中的方法2沒有被聲明爲虛擬的,哪種方法2?
提前致謝!
你試過了嗎? – Pubby 2013-03-01 04:55:15
'method2()'括號丟失,如果我沒有錯 – 2013-03-01 04:55:51
@Pubby嘗試的東西不一定會導致C++的啓發。事實上,如果發生未定義的行爲,它會導致危險的虛假安全感。 – Antimony 2013-03-01 04:56:31