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;
}

 

posted @ 2015-08-11 19:05  loverszhaokai  阅读(278)  评论(0编辑  收藏  举报