#include <iostream>
class A
{
};
int main()
{
std::cout << sizeof(A) << std::endl;
return 0;
}
1 对空类或只有成员函数的类(sizeof(Class)),其大小为1,这个1是存的什么?
什么也不存,但是一个类的对象既然要在内存里面存在,就一定要有一个地址,而在C++里面,编译器会给一个空类加一个成员比如char,这样,这个空类就不再为空,这个类的对象也就会有一个地址,可以被建立。
#include <iostream>
class A {};
struct B {};
int main() {
using namespace std;
cout << sizeof(A) << endl;
cout << sizeof(B) << endl;
}
g++:
1
1
bcc32:
8
8
vc:
1
1
bcc为什么是8 待续