1.当一个类只有构造函数和析构函数时 内存为 1
using namespace std;
class person
{
public:
person(){}
~person(){}
};
void main()
{
person p;
cout << sizeof(p)<<endl;// 当只有构造函数和析构函数时 内存为1 用来区分对象
}
2.当一个类有构造函数 和析构 函数时 还有虚函数时
编译器会设置一个指针来指向虚函数表(无论有多少个虚函数,只有一个虚函数表)
32位 中 内存为4
64位 中 内存为8
class man
{
public:
man() {}
~man() {}
virtual void show() = 0;
};
class son :public man
{
public:
son() {}
~son() {}
void show() { cout << " "; }
};
void main()
{
son p;
cout << sizeof(p)<<endl;//结果为4
}