static 函数 、const 函数小结(实战篇)

来源:互联网 发布:策略为王 源码下载 编辑:程序博客网 时间:2024/06/06 07:12

static 函数小结

#include<iostream>#include<stdio.h>using namespace std;class  point {    public:    void show(void)                        //普通方法可以访问访问static data     {        cout << "x = "<< x <<endl;        cout << "y =  "<< y<<endl;        cout<< " flag = "<<flag<<endl;    }    public:        point()        {}        point(int x,int y):x(x),y(y)        {}        point (point &sd)        {            x = sd.x;            y = sd.y;            flag = sd.flag;        }        point &operator = (point &sd)        {            if(this != &sd)            {                x= sd.x;                y = sd.y;                flag = sd.flag;            }            return *this;        }        ~point()        {}    private:        int x;        int y;        static int flag;      //静态数据,他不属于某一个对象的,因为他封闭了this指针                               //所有对象共享一份数据,};int point ::  flag  = 100;    //静态数据成员这样初始化,注:必须初始化int main(){      point a(1,3);      a.show();      return 0;}运行结果:x = 1y =  3 flag = 100Press any key to continue

1.static 成员数据 ,可以由普通方法来访问
2.static 必须初始化,并且初始化不同于普通变量初始化。
3.static 函数(封锁了this 指针) 只能访问static data 数据。

#include<iostream>#include<stdio.h>using namespace std;class  point {    public:/*  void show(void)                        //普通方法可以访问访问static data     {        cout << "x = "<< x <<endl;        cout << "y =  "<< y<<endl;        cout<< " flag = "<<flag<<endl;    }*/    static  void show(void )    {        cout << "flag = "<<flag<<endl;    }    public:        point()        {}        point(int x,int y):x(x),y(y)        {}        point (point &sd)        {            x = sd.x;            y = sd.y;            flag = sd.flag;        }        point &operator = (point &sd)        {            if(this != &sd)            {                x= sd.x;                y = sd.y;                flag = sd.flag;            }            return *this;        }        ~point()        {}    private:        int x;        int y;        static int flag;      //静态数据,他不属于某一个对象的,因为他封闭了this指针                               //所有对象共享一份数据,};int point ::  flag  = 100;    //静态数据成员这样初始化,注:必须初始化int main(){      point a(1,3);      a.show();      point ::show();      return 0;}运行结果:flag = 100flag = 100Press any key to continue

两种调用方式:

1 . 类名::static 函数
2 . 实例化一个对象.进行调用

#include<iostream>using namespace std;class Student{public:    Student(int n,float s):num(n),score(s){}    void change() const                         {       cout << "num =  "<<num++ <<endl;    //表示对象里的数据不能改变    }    void display() const    {        cout<<num<<"\t"<<score<<endl;    }    private:    const int num;    const  float score;} ;int main(){     Student const stud(101,78.5);    stud.display();    stud.change();    stud.display();    return 0;};Compiling...u.cppD:\VC++\Microsoft Visual Studio\MyProjects\uuu\u.cpp(66) : error C2166: l-value specifies **const object**执行 cl.exe 时出错.
#include<iostream>using namespace std;class Student{public:    Student(int n,float s):num(n),score(s){}    void change(int n,float s) const    {        num=n;        score=s;    }    void display() const    {        cout<<num<<"\t"<<score<<endl;    }    private:    mutable int num;    mutable float score;} ;int main(){     Student const stud(101,78.5);    stud.display();    stud.change(101,80.5);    stud.display();    return 0;};101     78.5101     80.5Press any key to continue
0 0