Created
February 20, 2020 15:18
-
-
Save imsjz/0ff5df406fbb00aef24035e8ee24bbb2 to your computer and use it in GitHub Desktop.
验证空类的大小,以及继承类的大小
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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