一.静态成员变量
#include <iostream>
using namespace std;
class Person
{
public:
static int m_A;//静态成员变量类内声明
};
int Person::m_A = 100;//类外初始化(要记得标注作用域)
void test01()
{
Person p1;
Person p2;
p1.m_A = 100;
p2.m_A = 200;
cout << p1.m_A << endl;//200--->共享同一份数据
}
int main()
{
test01();
return 0;
}
二.静态成员函数
#include <iostream>
using namespace std;
class Person
{
public:
static void func()
{
cout << "静态成员函数的调用" << endl;
}
};
void test01()
{
Person p;
p.func();//1.通过对象访问
Person::func();//2.通过类名访问
}
int main()
{
test01();
return 0;
}
注意:
与const区分,修饰函数时,const是+在后面的,static是+在前面的