#include<iostream>
#include<cstdio>
using namespace std;
// c++中只有非静态成员变量才占用对象空间;
class Person
{
public:
Person();
~Person();
int a; //非静态成员变量占对象空间;
static int b; //静态成员变量,不属于类对象上,即静态成员变量不占对象空间;
void fun() //函数也不占用对象空间,所有函数共享一个函数实例;
{
cout << "jjj" << endl;
}
static void fun2() //静态成员函数也不占用对象空间;
{
cout << "jjj" << endl;
}
private:
};
Person::Person()
{
}
Person::~Person()
{
}
int Person::b = 10;
void test()
{
Person p;
cout << sizeof(p) << endl;//1
}
int main()
{
test();
return 0;
}
成员变量和成员函数分开存储
最新推荐文章于 2024-07-25 12:39:48 发布