C++ 类在内存中的存储方式

空类

class Test {

};

Test t0;
cout << sizeof(t0) << endl;
// 运行结果:1

空类,没有任何成员变量和成员函数,编译器是支持空类实例化对象的,对象必须要被分配内存空间才有意义,这里编译器默认分配了 1Byte 内存空间(不同的编译器可能不同)

含有成员变量的类

// ====== 测试一 ======
class Test {
private:
  int i;      // 4 -> 4
  char c;     // 1 -> 4
  double d;   // 8 -> 8
};

Test t11;
cout << sizeof(t11) << endl;
// 运行结果:16 = 4+4+8

// ====== 测试二 ======
class A{};

class Test {
private:
  int i;      // 4 -> 4
  char c;     // 1 -> 4
  double d;   // 8 -> 8
  A a;        // 1 -> 8
};

Test t12;
cout << sizeof(t12) << endl;
// 运行结果:24 = 4+4+8+8

// ====== 测试三 ======
class A {
private:
  double dd;  // 8 -> 8
  int ii;     // 4 -> 8
  int* pp;    // x86: 4  x64: 8
};

class Test {
private:
  int i;      // 4 -> 8
  A a;        // 24 -> 24
  double d;   // 8 -> 8
  char* p;    // x86: 4  x64: 8
};

Test t13;
cout << sizeof(t13) << endl;
// x86 目标平台运行结果:40 = 8+(8+8+4)+8+4
// x64 目标平台下运行结果:48 = 8+(8+8+8)+8+8

解释:

含有成员变量和成员函数的类

// ====== 测试一 ======
class Test {
private:
  int n;    // 4 -> 4
  char c;   // 1 -> 2
  short s;  // 2 -> 2
};

Test t21;
cout << sizeof(t21) << endl;
// 运行结果:8

// ====== 测试二 ======
class Test {
public:
  Test() {
  }

  int func0() {
    return n;
  }

  friend int func1();

  int func2() const {
    return s;
  }

  inline void func3() {
    cout << "inline function" << endl;
  }

  static void func4() {
    cout << "static function" << endl;
  }

  ~Test() {
  }

private:
  int n;    // 4 -> 4
  char c;   // 1 -> 2
  short s;  // 2 -> 2
};

int func1() {
  Test t;
  return t.c;
}

Test t22;
cout << sizeof(t22) << endl;
// 运行结果:8

// ====== 测试三 ======
class Test {
public:
  Test() {
  }

  int func0() {
    return n;
  }

  friend int func1();

  int func2() const {
    return s;
  }

  inline void func3() {
    cout << "inline function" << endl;
  }

  static void func4() {
    cout << "static function" << endl;
  }

  virtual void func5() {
    cout << "virtual function" << endl;
  }           // x86: 4  x64: 8

  ~Test() {
  }

private:
  int n;      // 4 -> 4
  char c;     // 1 -> 2
  short s;    // 2 -> 2
};

int func1() {
  Test t;
  return t.c;
}

Test t23;
cout << sizeof(t23) << endl;
// x86 目标平台运行结果:12;x64 目标平台下运行结果:16

解释:

空类的派生类

// ====== 测试一 ======
class A {
};

class Test : public A{
private:
  char c;
};

Test test;
cout << sizeof(test) << endl;
// 运行结果:1

// ====== 测试二 ======
class A {
};

class Test : public A{
private:
    A a;
    char c;
};

Test test;
cout << sizeof(test) << endl;
// 在MinGW 64bit下编译运行结果:3;在MSVC2017 64bit下编译运行结果:2

解释:

基类有数据成员的派生类

// ====== 测试一 ======
class A {
private:
  double d;
  char c;
};

class Test : public A{
private:
  char c;
};

Test test;
cout << sizeof(test) << endl;
// 在 MinGW 64bit 下编译运行结果:16;在 MSVC2017 64bit 下编译运行结果:24

解释:

基类有虚函数的派生类

// ====== 测试一 ======
class A {
public:
    virtual void func() {
        cout << "class A virtual function" << endl;
    }

private:
    double d;
    char c;
};

class Test : public A{
private:
    char cc;
};

Test test;
cout << sizeof(test) << endl;
// 在MinGW 64bit下编译运行结果:24;在MSVC2017 64bit下编译运行结果:32

// ====== 测试二 ======
class A {
public:
  virtual void func() {
    cout << "class A virtual function" << endl;
  }

private:
  double d;
  char c;
};

class Test : public A{
public:
  virtual void func1() {
    cout << "class Test virtual function" << endl;
  }

private:
  char cc;
};

Test test;
cout << sizeof(test) << endl;
// 在MinGW 64bit下编译运行结果:24;在MSVC2017 64bit下编译运行结果:32

解释:

虚继承的派生类

// ====== 测试一 ======
class A {
private:
  double d;
  char c;
};

class Test : virtual public A{
private:
  char cc;
};

Test test;
cout << sizeof(test) << endl;
// 运行结果:32

解释:

总结

Table of Contents