静态成员函数
静态成员变量特点:
- 在编译阶段分配内存
- 类内声明,类外初始化
- 所有对象共享同一份数据
#include<iostream>
#include <string>
using namespace std;
class Person
{
public:
static void fun1()
{
m_A = 100;
cout << "静态成员函数static void fun1的调用 " << endl;
}
int m_B = 0;
static int m_A; //需要在类外定义
private:
static void fun2()
{
cout << "静态成员函数static void fun2的调用" << endl;
}
};
int Person::m_A = 0;
void test01()
{
//1、通过对象访问静态成员函数
Person p1;
p1.fun1();
//2、通过类直接访问
Person::fun1();
}
int main()
{
test01();
}