c++ - VTBL 是否包含指向非虚函数的指针?

标签 c++

<分区>

我很难理解为什么下面的代码打印 C::f2 而不是 B::f2。

From Charles Bailey answer:

When a non-virtual function is called the implementation must use the static type of the object on which the function is being called to determine the correct function to call. A function stored in a vtable accessed by a vptr will be dependent on the dynamic type of the object, not any static type of a reference or pointer through which it is being accessed.

我有点迷路了,在 A::f1 中有一个对 f2 的调用。编译器如何知道调用哪个方法?

我的假设:

  1. 编译器以某种方式记住我们在 C 类型的对象中。
  2. 编译器检查 C 是否包含非虚方法名 f2。 A。如果是,请运行它。 b.使用对象的指针访问他的 vtbl 并运行正确的 f2。

我说得对吗?

struct A
{
    void f1()
    {
        f2();
    }
    virtual void f2()
    {
        cout<<"A::f2"<<endl;
    }
};
struct B:public A
{
    virtual void f2()
    {
        cout<<"B::f2"<<endl;
    }
};
struct C:public B
{
    void f2()
    {
        cout<<"C::f2"<<endl;
    }
};
int main()
{
    C c1;
    c1.f1();
    return 0;
}

最佳答案

每个成员函数都有一个隐含的this 参数。 f1 中的this 的静态类型总是A * const。对于任何成员函数都是如此。隐式对象参数的静态类型是函数被定义的封闭类。

f1 中的调用解析为 this->f2()。由于这是一个通过指针的调用,函数 f2 是动态调度的。尽管 f1 不是虚拟的,并且将始终由静态调度调用。

无论编译器使用什么机制(VTable 是一个实现细节,不是 C++ 标准本身强制要求的),我们都会调用 C::f2

所以你的假设需要一些修改,我会说。


按照您在评论中指定的方式回答您的问题。 C::f2 是虚拟的。您可能在覆盖它时省略了 virtual 说明符,但是没有“取消虚拟化”它。

关于c++ - VTBL 是否包含指向非虚函数的指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45081391/

相关文章:

c++ - 数误差的质因数分解

c++ - QWidget setFocus 什么都不做

c++ - 没有合适的默认构造函数可用 - 默认构造函数在哪里调用?

c++ - 从 C++ 中的文件输入

c++ - 为什么当派生类调用基类的纯虚函数时 g++ 不报错?

c++ - 如何运行一个cgi程序

c++ - SFML 2.1 设置所有文本

c++ - 如何检测加密的文件?

c++ - 通过指针进行的内存复制有时会丢失数据

c++ - 如何在 Cocos2d-x 3.15.1 中避免 MenuItemImage 和 MenuItemSprite 冲突