c++ - C++ 中具有 int、函数、虚函数的 sizeof 类?

标签 c++ class object virtual sizeof

这是一个在线C++测试题,已经完成。

#include<iostream>
using namespace std; 
class A
{

};
class B
{
int i; 
}; 

class C
{
void foo();
};
class D
{
virtual void foo();
};

class E
{
int i ; 
    virtual void foo();
};
class F
{
int i; 
    void foo();
};
class G
{
    void foo();
    int i;
    void foo1();
};

class H
{
    int i ;
    virtual void foo();
    virtual void foo1();
};
int main()
{
cout <<"sizeof(class A) : " << sizeof(A) << endl ;
cout <<"sizeof(class B) adding the member int i : " << sizeof(B) << endl ;
cout <<"sizeof(class C) adding the member void foo() : " << sizeof(C) << endl ;
cout <<"sizeof(class D) after making foo virtual : " << sizeof(D) << endl ;
cout <<"sizeof(class E) after adding foo virtual , int : " << sizeof(E) << endl ;
cout <<"sizeof(class F) after adding foo  , int : " << sizeof(F) << endl ;
cout <<"sizeof(class G) after adding foo  , int : " << sizeof(G) << endl ;
G g;
cout <<"sizeof(class G) after adding foo  , int : " << sizeof(g) << endl ;
cout <<"sizeof(class H) after adding int 2 virtual " << sizeof(H) << endl ;
return 0; 
}

输出:

sizeof(class A) : 1
sizeof(class B) adding the member int i : 4
sizeof(class C) adding the member void foo() : 1
sizeof(class D) after making foo virtual : 8
sizeof(class E) after adding foo virtual , int : 16
sizeof(class F) after adding foo  , int : 4
sizeof(class G) after adding foo   , unsigned int : 4
sizeof(class g) after adding foo  , unsigned int : 4
sizeof(class H) after adding int 2 virtual 16

我的问题:

为什么 siszeof(A) 是 1 而 sizeof(C) 也是 1 ?

为什么 siszeof(H) 是 16 而 sizeof(G) 是 4 ?

为什么 siszeof(E) 是 16 而 sizeof(F) 是 4 ?

为什么 siszeof(D) 是 8 而 sizeof(E) 是 16 ?

我的猜测:

虚函数是一个8字节的指针。 但是,我不知道为什么 E 的大小是 16 ? 向空类添加函数不会改变其大小?

感谢任何帮助。

谢谢

最佳答案

首先,虚函数不是一个 8 字节的指针。在 C++ 中,除了 sizeof(char) 之外什么都没有保证是任意数量的字节。

其次,只有类中的第一个虚函数会增加它的大小(依赖于编译器,但在大多数情况下——如果不是全部的话——都是这样的)。所有后续方法都没有。非虚函数不影响类的大小。

发生这种情况是因为类实例不保存指向方法本身的指针,而是指向虚拟函数表,每个类一个。

如果你有:

class A
{
   virtual void foo();
}

class B
{
   virtual void goo();
   virtual void test();
   static void m();
   void x();
}

你会有 sizeof(A) == sizeof(B)

现在:

Why siszeof(A) is 1 and sizeof(C) is 1 too ?

AC 的大小为 1,只是因为不允许类的大小为 0。函数与它无关。它只是一个虚拟字节。

Why siszeof(H) is 16 but sizeof(G) is 4 ?

G 只有一个负责内存的成员 - int。在您的平台上,sizeof(int) == 4H,除了int,还有一个指向vftable的指针(虚函数表,见上)。 this 的大小、int 的大小和对齐方式是特定于编译器的。

Why siszeof(E) is 16 but sizeof(F) is 4 ?

上面解释过——非虚方法不占用类中的内存。

Why siszeof(D) is 8 but sizeof(E) is 16 ?

D 仅包含 vftable 指针,在您的平台上显然是 8 个字节。 E 也有一个 int,vftable 对齐到 8 个字节。所以它是这样的:

class E

4 bytes for int |  4 padding bytes  |  8 bytes for vftable pointer  | 
| x | x | x | x |    |    |    |    | v | v | v | v | v | v | v | v |

关于c++ - C++ 中具有 int、函数、虚函数的 sizeof 类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9439240/

相关文章:

c# - C# 中的三层应用程序 - 放置数据和业务模型的位置

javascript - 查找数组中是否存在对象中的值

Javascript/AJAX/jQuery - 对象预期行 1 仅在 IE 中

java - 在 Java 中哈希 double

C++ boost函数问题

c++ - 为什么消息框不阻塞线程?

c++ - 部分填充模板作为模板template的参数

c++ - C++ 链接步骤中的 undefined reference 错误

python - Python 中的静态类变量和 `self`

c++ - iostream 和 fstream 的重载 <<