【发布时间】:2014-07-21 12:59:30
【问题描述】:
谁能解释一下为什么sizeof 函数在下面的代码中返回不同的值?
//static member
class one
{
public :
static const int a = 10;
};
//non static member
class two
{
public :
int a;
};
int main()
{
cout << sizeof(one); //print 1 to lcd
cout << sizeof(two); //print 4 to lcd,differ from size of one class
}
【问题讨论】:
-
静态成员每个类存在一次(并且不属于任何实例),非静态成员每个实例存在一次。因此,类型 1 的对象不包含名称为 a 的字段,类型 2 的对象包含。