#include <iostream>
using namespace std;
class Person {
public:
//静态成员函数
static void func() {
m_A = 100;//静态成员函数可以访问 静态成员变量
//m_B = 200;//静态成员函数不可以访问 非静态成员变量,无法区分是哪一个对象的
cout << "static func调用" << endl;
}
static int m_A;//静态
int m_B;//非静态
//静态成员函数也是有访问权限的
private:
static void func2() {
cout << "ststic void func2调用" << endl;
}
};
int Person::m_A = 0;
void test01() {
//通过对象访问
Person p;
//通过类访问
p.func();
Person::func();
//Person::func2();类外访问不到私有静态成员函数
};
int main() {
test01();
return 0;
}
类和对象-静态成员
于 2023-11-02 18:01:15 首次发布