Skip to content

Instantly share code, notes, and snippets.

@imsjz
Created February 20, 2020 15:18
Show Gist options
  • Save imsjz/0ff5df406fbb00aef24035e8ee24bbb2 to your computer and use it in GitHub Desktop.
Save imsjz/0ff5df406fbb00aef24035e8ee24bbb2 to your computer and use it in GitHub Desktop.
验证空类的大小,以及继承类的大小
#include <iostream>
using namespace std;
class X {};
class Y : public virtual X {};
class Z : public virtual X {};
class A : public Y, public Z{};
int main() {
cout << sizeof(X) << endl; // 1, 编译器会插入一个char字符,因此是1个字节
X a, b;
if(&a == &b)
cerr << "yipes" << endl;
cout << sizeof(Y) << endl; // 8
cout << sizeof(Z) << endl;
cout << sizeof(A) << endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment