和编译器的具体实现有关系
C++标准上给出了一段说明,在1.8 The C++ Memory Model中
2 Objects can contain other objects, called sub-objects.
A sub-object can be a member sub-object (9.2), a base class
sub-object (clause 10), or an array element. An object that is
not a sub-object of any other object is called a complete object.
4 If a complete object, a data member (9.2), or an array element
is of class type, its type is considered the most derived class,
to distinguish it from the class type of any base class subobject;
an object of a most derived class type is called a most derived object.
5 Unless it is a bit-field (9.6), a most derived object shall have
a non-zero size and shall occupy one or more bytes of storage. Base
class sub-objects may have zero size. An object of POD 4) type (3.9)
shall occupy contiguous bytes of storage.
因此在实现上,即使是一个空的类对象,至少也要分配1个字节的空间,而且不同的编译器对此有不同的实现,如下:
#include <iostream>
using namespace std;
class X
{
};
int main(int ,char **)
{
X x;
cout << "size of X:" << sizeof (X) << endl;//VC中值为1,BCB中值为8
cout << "size of x:" << sizeof (x) << endl;//VC中值为1,BCB中值为8
cout << "all right!" << endl;
cin.get();
return 0;
}