zoukankan      html  css  js  c++  java
  • 类中静态成员函数的使用

    类中静态成员函数一般对静态成员调用  ,而要调用其非静态成员时,则类似于函数形参引用类一样(然其有一种情形,即不建立类对象,亦可引用静态成员函数,如: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<<" "<<"b="<<w.b<<" "<<"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;
    }
     
     
     
     
     
     
     
     
     
  • 相关阅读:
    红黑树——以无厚入有间
    红黑树——依天理以神遇
    B-树 分合之道
    B-树 动机与结构
    云心出岫——Splay Tree
    双散列和再散列暨散列表总结
    开放定址法——平方探测(Quadratic Probing)
    [LeetCode 109]
    [LeetCode 110]
    [LeetCode 111]
  • 原文地址:https://www.cnblogs.com/tangjunjun/p/11676674.html
Copyright © 2011-2022 走看看