具有虚拟功能的类的大小
Size of class with virtual function
我正在修改C++概念,但我被困在一个非常简单的代码中
#include <iostream>
using namespace std;
class foo {
public:
//int i;
void virtual foobar()
{
cout << "foobarn";
}
};
int main()
{
foo f;
cout << sizeof(f) << endl;
//cout << sizeof(f.i) << endl;
return 1;
}
上述代码的输出为 8但是当我从代码中删除注释时输出为 16 和 4
我不明白当类不存在成员变量时,VPTR 大小为 8,但在添加变量大小后变为 12。
您正在使用指针对齐到 8 个字节的平台。由于虚拟表指针通常是对象布局中的第一件事,因此它也必须与 8 个字节对齐。因此,在 int
成员之后插入填充 4 个字节,这就是为什么您的大小为 16(vf 表指针为 8 个字节,int 为 4 个字节,4 个填充字节)。
相关文章:
- 如何在C++中获得"静态纯虚拟"功能?
- std::is_trivially_copyable_v 关于虚拟功能
- 类似虚拟的朋友功能?
- 是否有任何模式等效于虚拟模板功能?
- 隐藏重载虚拟功能的模板化访客:SFINAE 在使用?
- 为什么我无法覆盖虚拟功能?
- 二叉搜索树 - 使用 Linux 在虚拟机中制作删除功能
- 虚拟功能不起作用
- 如果在创建对象时创建了 VPTR,那么为什么具有虚拟功能的类的大小在 32 位系统上为 4,在 64 位机器上为 8
- 虚拟功能如何影响static_cast?
- __declspec(属性)和虚拟功能
- 实现来自第三方类的虚拟功能
- 消除未使用的虚拟功能
- 为什么虚拟继承即使不涉及虚拟功能也需要 vtable?
- C 虚拟功能的输出
- 抽象类 A 由类 C 继承"through"类 B "intermediary"。如何声明虚拟功能?
- 虚拟功能是否不太可能导致堆栈溢出
- 使用继承和虚拟功能应用五个规则
- 父会员功能中虚拟函数的C 行为
- 在多态性中重新定义功能(虚拟),具有不同数量的参数