码迷,mamicode.com
首页 > 编程语言 > 详细

C++ 对象的sizeof问题

时间:2015-08-11 21:23:35      阅读:126      评论:0      收藏:0      [点我收藏+]

标签:

需要补充。。

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

 

C++ 对象的sizeof问题

标签:

原文地址:http://www.cnblogs.com/lovers/p/4721891.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!