可能重複:
Calling virtual functions inside constructorsC++:從基礎構造函數調用函數?
class Base
{
virtual void method()
{ cout << "Run by the base."; };
public:
Base() { method(); };
};
class Derived: public Base
{
void method()
{ cout << "Run by the derived."; };
};
void main()
{
Derived();
}
輸出:
Run by the base.
怎麼能一個已經運行,而不是派生的方法,未做派生的構造函數?
-1這個問題在SO上已經問了很多次,你可以很容易地找到它。 – 2010-07-02 14:25:07
我試過了,但我沒想過要搜索「虛擬」這個詞。無論如何,抱歉浪費你的時間,如果你想讓我關閉它,告訴我如何,但我認爲這個例子總結得很好。 – 2010-07-02 14:29:24
只是谷歌「從構造函數調用虛函數」你會發現**噸**有用的信息。我希望我可以結束由於「首先使用Google」 – Andrey 2010-07-02 14:48:53