加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c对象大小

发布时间:2020-12-16 07:52:44 所属栏目:百科 来源:网络整理
导读:假设我有: struct Foo: public Bar { ....} Foo不介绍新的成员可变. Foo只引入了一堆成员函数静态功能. C标准的任何部分现在都保证: sizeof(Foo) == sizeof(Bar) ? 谢谢! 解决方法 我可以想到至少一个场景,其中sizeof(Foo)!= sizeof(Bar): class Bar {
假设我有:
struct Foo: public Bar {
 ....
}

Foo不介绍新的成员可变. Foo只引入了一堆成员函数&静态功能. C标准的任何部分现在都保证:

sizeof(Foo) == sizeof(Bar)

谢谢!

解决方法

我可以想到至少一个场景,其中sizeof(Foo)!= sizeof(Bar):
class Bar {
public:
 int m_member;
};

class Foo : Bar {
    virtual ~Foo();
};

Foo会有一个vtable指针,而Bar不会,而前者的大小将是Bar的一个字.在32位DEBUG MSVC2010:

sizeof(Foo) - 8
sizeof(Bar) - 4

编辑这适用于结构体和类,我重新进行测试以确认.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读