学习完静态成员变量,趁热打铁,学习静态成员函数,其基本性质及使用如下:
项目结构如下: 代码及注释如下: CIntreger.h:
class CInteger
{
public:
CInteger(int nNumber) {
m_nNumber = nNumber;
}
int GetNumber()const;
void SetNumber(int nNumber) {
m_nNumber = nNumber;
}
static void foo();
static int m_nStatic;
private:
int m_nNumber;
};
CIntreger.cpp:
#include "CIntreger.h"
#include <iostream>
int CInteger::GetNumber()const
{
return m_nNumber;
}
int CInteger::m_nStatic=123;
void CInteger::foo()
{
printf("CInteger::foo()");
m_nStatic = 2;
}
TestC11.cpp:
#include <iostream>
#include "CIntreger.h"
int main()
{
CInteger i = 1;
i.foo();
CInteger::foo();
CInteger::m_nStatic = 1;
int nSize = sizeof(CInteger);
return 0;
}
视频讲解地址:B站视频
|