简体   繁体   English

通过派生类的实例在基中调用纯虚函数

[英]Calling pure virtual function in base through instance of derived class

Will the following code require a vtable lookup or can the compiler foresight, which function to call? 以下代码是否需要vtable查找,或者编译器可以预见,该调用哪个函数?

class Base
{
    virtual void foo() = 0;

    void bar() {
        this->foo();   // [*]
    };
};

class A : Base
{
    void foo() final {
        ...
    };
};

Base *base = new A();
base->bar();     // sure vtable lookup at [*]!


A a;
a.bar();         // vtable lookup at [*]?

The answer is that there is nothing in the C++ standard that would require dynamic dispatch in either of the two invocations. 答案是,在C ++标准中,没有任何两个调用中的任何一个都需要动态分配。 If an implementation can prove what function will be called at runtime, it may dispense with dynamic dispatch. 如果实现可以证明在运行时将调用什么函数,则可以免除动态分配。 For instance, if the call to bar is inlined, the implementation may be made aware that it is invoking foo on an object whose most derived type is A . 例如,如果对bar的调用是内联的,则可以使实现意识到它正在对最派生类型为A的对象调用foo In such a case it doesn't need to dispatch dynamically. 在这种情况下,它不需要动态调度。

For the cases where the call to bar is not inlined, the static type of this inside bar will be Base* . 对于没有内联的bar调用的情况, this内部bar的静态类型将为Base* Without any knowledge of the most derived object type and the calling context, and given the indirection, an implementation will need to do dynamic dispatch to guarantee correct behavior. 在没有任何关于最派生对象类型和调用上下文的知识的情况下,并且在没有间接信息的情况下,实现将需要进行动态分配以保证正确的行为。

If you want to know how an implementation handles a concrete piece of code, then you may examine the generated assembly, perhaps with a tool like https://godbolt.org . 如果您想知道实现如何处理具体的代码,则可以使用https://godbolt.org之类的工具检查生成的程序集。 It should make it clear as day what sort of call is being made. 应该在一天中清楚地说明正在拨打哪种电话。

First of all the, you must use protected or public access class modifier before functions definitions . 首先,必须在函数定义之前使用受保护的或公共访问的类修饰符。 In other case private is the default access modifier and you must set class A : public Base. 在其他情况下,private是默认的访问修饰符,并且您必须设置A类:public Base。 In addition to this, in the first case in run time the entry in vtable will be use to find the foo function in dervived class (dynamic dispatch). 除此之外,在运行时的第一种情况下,vtable中的条目将用于在派生类(动态调度)中查找foo函数。 in the second case the function bar is inherited from class A. No dynamic dispatch in this case – getsoubl 在第二种情况下,功能栏是从类A继承的。在这种情况下,不进行动态分派– getsoubl

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

相关问题 使用基类中的函数静态调用纯虚函数的派生类实现 - Calling a derived-class implementation of a pure virtual function statically using a function in the base class 在派生类对象作为参数的基类中声明纯虚函数 - Declaring pure virtual function in base class with derived class object as arguments 派生类的构造函数调用的函数不应在基类中是纯虚拟的 - function called by constructors of derived classes not supposed to be pure virtual in base class 返回类型为基类/派生类型的抽象类中的纯虚函数 - Pure virtual function in abstract class with return type of base/derived type C ++继承:使用派生类的实例调用基类的虚函数 - C++ Inheritance: Calling the virtual function of a base class using an instance of a derived class 从抽象基类成员函数调用纯虚函数? - Calling Pure Virtual Function From Abstract Base Class Member Function? 纯虚拟基础方法在派生类中的专业化 - Specialization of pure virtual base method in derived class 使用没有虚拟的模板从基类调用派生类函数 - Calling derived class function from base class with templates without virtual 从基础 class 构造函数调用派生 class 的虚拟 function? - Calling virtual function of derived class from base class constructor? 重载虚拟函数并通过指向基类的指针调用派生函数 - overloading virtual function and calling derived function by pointer to base class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM