简体   繁体   English

从基础 class 构造函数调用派生 class 的虚拟 function?

[英]Calling virtual function of derived class from base class constructor?

I´m trying to accomplish the same which is described in a previous question:我正在尝试完成上一个问题中描述的相同操作:

virtual function call from base class 从基础 class 调用虚拟 function

But, my real question is:但是,我真正的问题是:

What if f() is the constructor in the Base class?如果 f() 是 Base class 中的构造函数怎么办? Which g() will be called?哪个 g() 将被调用? I don´t know if I am doing wrong, but in my program it seems to be that is the opposite.我不知道我是否做错了,但在我的程序中似乎恰恰相反。

Taking the same variables from the previous question, a code which shows such从上一个问题中获取相同的变量,一个显示这样的代码

behavior would look like this:行为看起来像这样:

Class Base

{   

    Base(){g();};

    virtual void g(){//Do some Base related code;}

};



Class Derived : public Base

{   

    Derived(){};

    virtual void g(){//Do some Derived related code};

};



int main()

{

    Derived newDerived;

    return 0;  

}

Update:更新:

Thanx to Naveen.感谢纳文。

He provided me a page which contains all related information about this topic.他为我提供了一个页面,其中包含有关该主题的所有相关信息。

I´ll let you know the link here:我会让你知道这里的链接:

parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.6parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.6

Even though it's a virtual function, the base's version will get called since the derived class isn't fully constructed yet.即使它是虚拟 function,也会调用基础版本,因为派生的 class 尚未完全构建。 The base class constructor is called before the derived class constructor, so if the derived virtual function were to get called, it would be with an incompletely-initialized instance - a possible (probably) recipe for disaster.在派生的 class 构造函数之前调用基础 class 构造函数,因此如果要调用派生的虚拟 function,它将使用未完全初始化的实例 - 可能的(可能)

It will Base::g() .它将Base::g() See this FAQ for explanation.有关说明,请参阅此常见问题解答

The virtual mechanism does not work in the constructors, so if you call even a virtual function from a base class constructor you will always end up calling the functions of the base class only.虚拟机制在构造函数中不起作用,因此如果您从基本 class 构造函数调用甚至是虚拟 function,您最终将始终只调用基本 class 的函数。 There are a few reasons why the virtual funcs do not work in the ctors:虚拟函数在 ctor 中不起作用的原因有几个:

  1. While in the constructors the object has not been created fully.虽然在构造函数中 object 尚未完全创建。
  2. ctors calls are resolved at compile time only, so they actually don't have any runtime dependency, so no use of virtual functions. ctors 调用仅在编译时解析,因此它们实际上没有任何运行时依赖性,因此不使用虚函数。
  3. Unlike other functions ctors and dtors are not inherited, so every class has its own set of ctors and dtors, so there is no chance of overriding.与其他函数不同,ctors 和 dtors 不会被继承,因此每个 class 都有自己的一组 ctors 和 dtors,因此没有覆盖的机会。

When your base class constructor is called, only the vtable for the base class is setup, so any virtual function calls will apply only to base class methods.当调用基本 class 构造函数时,仅设置基本 class 的 vtable,因此任何虚拟 function 调用将仅适用于基本 class 方法。

When the derived class constructor is called, calling a virtual function will call the derived class override, if any.当调用派生的 class 构造函数时,调用虚拟 function 将调用派生的 class 覆盖(如果有)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM