纯虚函数位于 C++ 中的什么位置?

发布于 2024-08-27 00:55:49 字数 213 浏览 7 评论 0原文

纯虚函数位于哪个虚表?在基类还是派生类中?

例如,每个类中的虚拟表是什么样的?

class Base {

  virtual void f() =0;
  virtual void g();
}


class Derived: public Base{

  virtual void f();
  virtual void g();

}

Which virtual table will be pure virtual function located? In the base class or derived class?

For example, what does the virtual table look like in each class?

class Base {

  virtual void f() =0;
  virtual void g();
}


class Derived: public Base{

  virtual void f();
  virtual void g();

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

指尖上的星空 2024-09-03 00:55:49

g++ -fdump-class-hierarchy layout.cpp 生成一个文件 layout.cpp.classlayout.cpp.class 的内容将显示以下内容:

Vtable for Base
Base::_ZTV4Base: 4u entries
0     (int (*)(...))0
8     (int (*)(...))(& _ZTI4Base)
16    __cxa_pure_virtual
24    Base::g

Class Base
   size=8 align=8
   base size=8 base align=8
Base (0x7ff893479af0) 0 nearly-empty
    vptr=((& Base::_ZTV4Base) + 16u)

Vtable for Derived
Derived::_ZTV7Derived: 4u entries
0     (int (*)(...))0
8     (int (*)(...))(& _ZTI7Derived)
16    Derived::f
24    Derived::g

Class Derived
   size=8 align=8
   base size=8 base align=8
Derived (0x7ff893479d90) 0 nearly-empty
    vptr=((& Derived::_ZTV7Derived) + 16u)
  Base (0x7ff893479e00) 0 nearly-empty
      primary-for Derived (0x7ff893479d90)

删除 f 的“纯粹性”将第五行更改为:

16    Base::f

g++ -fdump-class-hierarchy layout.cpp produces a file layout.cpp.class. The content of layout.cpp.class will show the following:

Vtable for Base
Base::_ZTV4Base: 4u entries
0     (int (*)(...))0
8     (int (*)(...))(& _ZTI4Base)
16    __cxa_pure_virtual
24    Base::g

Class Base
   size=8 align=8
   base size=8 base align=8
Base (0x7ff893479af0) 0 nearly-empty
    vptr=((& Base::_ZTV4Base) + 16u)

Vtable for Derived
Derived::_ZTV7Derived: 4u entries
0     (int (*)(...))0
8     (int (*)(...))(& _ZTI7Derived)
16    Derived::f
24    Derived::g

Class Derived
   size=8 align=8
   base size=8 base align=8
Derived (0x7ff893479d90) 0 nearly-empty
    vptr=((& Derived::_ZTV7Derived) + 16u)
  Base (0x7ff893479e00) 0 nearly-empty
      primary-for Derived (0x7ff893479d90)

Removing the 'pureness' of f changes the fifth line to:

16    Base::f
笛声青案梦长安 2024-09-03 00:55:49

每个类都有自己的 vtable。 Base 中的 f 条目将为 NULLDerived 中的条目将是指向代码的指针对于实施的方法。

Each class has its own vtable. The entry for f in Base will be NULL, and the entry in Derived will be a pointer to the code for the implemented method.

得不到的就毁灭 2024-09-03 00:55:49

vtable 条目将位于基类中。

为什么?因为您可以拥有一个保存派生类型对象地址的基指针类型,并且仍然调用基类型的指针变量上的方法。

纯虚函数只是告诉编译器,派生类型必须提供自己的实现,并且不能依赖基类的实现(如果基类中指定了实现)

The vtable entry will be in the base class.

Why? Because you can have a base pointer type that holds a derived type object's address and still call the method on the base type's pointer variable.

Pure virtual simply tells the compiler that the derived types must provide their own implementation, and they cannot rely on the base class' implementation (if one is even specified in the base class)

时光沙漏 2024-09-03 00:55:49

实际上两者都是。基类 vtable 将有一个用于纯虚函数的插槽,该插槽指向类似 pure_virtual_function_used() 存根的内容,该存根可能会中止程序,而派生类 vtable 将有一个指向实际实现的指针。

In both actually. The base class vtable will have a slot for the pure virtual function pointing to something like pure_virtual_function_called() stub that would probably abort the program, while the derived class vtable will have a pointer to the actual implementation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文