#include<iostream>
#include<string>
#include<vector>
using namespace std;
#include<algorithm>
#include<functional>
#include<ctime>
//静态成员就是在成员变量和成员函数之前加上关键字static
class Person
{
public:
//所有对象都共享同一份数据
//编译阶段就分配内存
//类内声明,类外初始化操作
static int m_A;
//静态成员变量也是有访问权限的
private:
static int m_B;
};
int Person::m_A = 100;
void test01()
{
Person p;
cout << p.m_A << endl;
Person p2;
p2.m_A = 200;
//100?200 ->200
cout << p.m_A << endl;
}
void test02()
{
//静态成员变量不属于某个对象上,所有对象都共享一份数据
//因此静态成员变量有两种访问方式
//通过对象进行访问
Person p;
cout << p.m_A << endl;
//通过类名进行访问
cout << Person::m_A << endl;
//静态成员变量也是有访问权限的 private
//cout << Person::m_B << endl;
}
int main()
{
test02();
}
静态成员变量
最新推荐文章于 2023-09-22 22:00:13 发布