除了其他优秀的答案之外,有一件事非常重要,通常被忽略/遗忘,或被误解(这就是为什么我详细说明下面的过程):
>在Java中,方法是虚拟的,即使从构造函数调用(可能会导致错误)
>在C中,从构造函数调用时,虚拟方法不是虚拟的(这可能会导致误解)
什么?
让我们想象一个Base类,一个虚拟方法foo()。
>让我们想象一个派生类,从Base继承,覆盖方法foo()
C和Java之间的区别是:
>在Java中,从Base类构造函数调用foo()将调用Derived.foo()
>在C中,从Base类构造函数调用foo()将调用Base.foo()
为什么?
每种语言的“错误”都是不同的:
>在Java中,在构造函数中调用任何方法可能会导致微妙的错误,因为重写的虚拟方法可能会尝试访问在Derived类中声明/初始化的变量。
Conceptually, the constructor’s job is to bring the object into existence (which is hardly an ordinary feat). Inside any constructor, the entire object might be only partially formed – you can know only that the base-class objects have been initialized, but you cannot know which classes are inherited from you. A dynamically-bound method call, however, reaches “forward” or “outward” into the inheritance hierarchy. It calls a method in a derived class. If you do this inside a constructor, you call a method that might manipulate members that haven’t been initialized yet – a sure recipe for disaster.
Bruce Eckel, 07000
>在C中,必须记住,虚拟机将无法正常工作,因为只有当前构造的类的方法将被调用。原因是避免访问数据成员,甚至是不存在的方法。
During base class construction, virtual functions never go down into derived classes. Instead, the object behaves as if it were of the base type. Informally speaking, during base class construction, virtual functions aren’t.
Scott Meyers, 07001