//HW_CH03_22(1)
//使用静态成员函数
#include<iostream>
#include<string.h>
using namespace std;
class person {
public:
char m_strName[20];
long m_ID;
public:
person( const char* strName, long ID) {
strcpy_s(m_strName, strName);
m_ID = ID;
}
/*
static long GetID() {
return m_ID;
}
原题中为这个,静态成员函数只能访问类的静态成员,而不能访问类的非静态成员
解决方法:将对象作为静态成员函数的参数,然后在静态成员函数中通过对象访问他的非静态成员
*/
static long GetID(person a ) {
return a.m_ID;
}
};
int main() {
person person1("LiuJun", 1101640524);
cout << "ID=" << person::GetID(person1) << '\n';
}
使用静态成员函数
最新推荐文章于 2023-09-22 23:01:02 发布