C++ 对象的sizeof问题
需要补充。。
1. 注意虚函数的指针占4个字节。(当然是32位机器)
#include <cstdlib> #include <ctime> #include <iostream> #include <string.h> using namespace std; class A { public: const int a; }; class B { }; class C { public: ~C() {} }; class D : public C { public: ~D() {} }; class E { public: void print() {}; }; class F : public E { public: void print() {}; }; class G { public: virtual void print() {}; }; class H : public G { public: void print() {}; }; int main() { cout << "sizeof(A) = " << sizeof(A) << endl; // 4 cout << "sizeof(B) = " << sizeof(B) << endl; // 1 cout << "sizeof(C) = " << sizeof(C) << endl; // 1 cout << "sizeof(D) = " << sizeof(D) << endl; // 1 cout << "sizeof(E) = " << sizeof(E) << endl; // 1 cout << "sizeof(F) = " << sizeof(F) << endl; // 1 cout << "sizeof(G) = " << sizeof(G) << endl; // 8 cout << "sizeof(H) = " << sizeof(H) << endl; // 8 return 0; }
作者:loverszhaokai
出处:http://www.cnblogs.com/lovers
本文采用知识共享署名-非商业性使用-相同方式共享 2.5 中国大陆许可协议进行许可,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。