class Person
{
public:
Person();
~Person();
static int m_Age; //加入static就是 静态成员变量,会共享数据,在类内声明,类外进行初始化
//不可以访问,普通成员变量
static void func()
{
cout << "静态成员函数 func" << endl;
}
void myFunc()
{
cout << "m_Orher: " << m_Orher << endl;
}
private:
static int m_Orher;
static void func2() {
cout << "私有的静态成员函数 func2" << endl;
}
};
int Person::m_Age = 10; //类外初始化实现
int Person::m_Orher = 10; //私有静态变量实现
Person::Person()
{
}
Person::~Person()
{
}
void test01()
{
//通过对象访问属性
Person p1;
p1.m_Age = 10;
Person p2;
p2.m_Age = 20;
cout << "p1 = " << p1.m_Age << endl;
cout << "p2 = " << p2.m_Age << endl;
cout << "通过类名直接访问: " << Person::m_Age << endl;
p1.func();
p2.func();
Person::func();
p1.myFunc();
//Person::func2();
}
原文:https://www.cnblogs.com/lodger47/p/14692468.html