类中静态成员函数的使用

类中静态成员函数一般对静态成员调用  ,而要调用其非静态成员时,则类似于函数形参引用类一样(然其有一种情形,即不建立类对象,亦可引用静态成员函数,如:STATIC_A::disp( );),其代码如下:
 
#include "stdafx.h"
#include
using namespace std;
class STATIC_A{
public:
STATIC_A ();
~STATIC_A (){};
void plus(){
c=c+100;
};
static void disp(STATIC_A &w);
public:
int a;
float b;
 static int c;
};
int STATIC_A::c=100;
STATIC_A::STATIC_A (){       //构造函数一般给变量一个初始值
a=100;
b=10*a;
};
 void STATIC_A::disp(STATIC_A &w){  
w.plus();                           //调用类中普通成员函数
cout<<"a="<<w.a<<"\t"<<"b="<<w.b<<"\t"<<"c="<<c<<endl; //注意c变量与a、b变量使用异同
};
int _tmain(int argc, _TCHAR* argv[])
{
const int t=6;
STATIC_A A[t];   //使用数组结构的类
for (int i=0;i
A[i].disp(A[i]); //执行静态成员函数
}
while (1);
return 0;
}
 
 
 
 
 
 
 
 
 
posted @ 2019-10-15 11:35  tangjunjun  阅读(998)  评论(0编辑  收藏  举报
https://rpc.cnblogs.com/metaweblog/tangjunjun