#include <iostream>
using namespace std;
class Demo
{
private:
int x;
double y;
};
int main()
{
cout << sizeof(Demo) << endl;
cout << sizeof(char) << endl;
cout << sizeof(int) << endl;
cout << sizeof(double) << endl;
cout << sizeof(Demo*) << endl;
cout << sizeof(char*) << endl;
cout << sizeof(int*) << endl;
cout << sizeof(double*) << endl;
return 0;
}
使用sizeof() 函数。
如果类Demo中的double为指针的话,则类Demo的大小为8 。为double类型的话,则类Demo的大小为16,不知道原因。