#include <iostream>
using namespace std;
class person
{
public:
int age;
static int height;
static void printf(void)
{
cout << "printf" << endl;
}
};
//静态成员函数有两种方法,一种是直接在类里面定义,一种是在类外面定义
/*
void person::printf(void)
{
cout << "printf" << endl;
}
*/
//静态成员变量与普通成员变量的区别,
int person::height = 1; //需要在外部重新初始化
int main(void)
{
person p1;
p1.age = 23;
cout << "age=" << p1.age << endl;
person p2;
p2.height = 23;
cout << "height=" << p2.height << endl;
//静态成员不需要对象就可以重新访问
person::height = 24;
cout << "height=" << person::height << endl;
getchar();
return 0;
}
类的静态成员
最新推荐文章于 2021-12-29 16:46:32 发布