有没有一种计算c ++类大小的方法。
当我在此类下创建此对象时,对象的大小为24字节。

class student
{
  student* pointer;
  char * c;
  student* AnotherPointer;
  class SubClass
  {
    int a,b;
  };
};


现在从学生班级删除子类,我仍然得到学生班级对象的大小为24。
为什么它没有改变?

class student
{
  student* pointer;
  char * c;
  student* AnotherPointer;
};

最佳答案

有没有一种计算c ++类大小的方法。


您可以使用sizeof运算符来获取类或任何其他类型的大小。例如:

std::cout << sizeof(student);



  删除子类


请注意,名为Subclass的类不是“子类”。这是一个嵌套类。


  为什么它没有改变?


因为您没有对类的子对象进行任何更改。嵌套类不是子对象。实际上,它根本不是对象-它是一种类型。

关于c++ - C++中用户定义的类的大小,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58477965/

10-13 07:01