可能重复:
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