Fork me on GitHub

C++——static & const

静态成员

由关键字static修饰说明的类成员,称为静态类成员(static class member)。虽然使用static修饰说明,但与函数中的静态变量有明显差异。类的静态成员为其所有对象共享,不管有多少对象,静态成员只有一份存于公用的内存中。

静态成员又分为静态成员函数,静态成员数据

静态数据

#include<iostream>
using namespace std;

class Test
{
    friend void fun(Test &t);
public:
    Test(int d=0)
    {
        count++;
        data = d;
    }
    ~Test()
    {
        count--;
    }
private:
    int data;
    static int count;
};

int Test::count = 0;

void fun(Test &t)
{
    cout << "t.data = " << t.data << endl;
    cout << "Object count = " << Test::count << endl;
}

int main()
{
    Test t1(100);
    Test t2(200);
    fun(t1);
}
View Code

静态成员数据在类外初始化

静态函数

#include<iostream>
using namespace std;

class Test
{
    friend void fun(Test &t);
public:
    Test(int d=0)
    {
        count++;
        data = d;
    }
    ~Test()
    {
        count--;
    }
    void show1()
    {
        cout << "data = " << data << endl;
        cout << "Object count = " << Test::count << endl;
    }
    static void show2()
    {
        cout << "Object count = " << Test::count << endl;
    }
private:
    int data;
    static int count;
};

int Test::count = 0;

void fun(Test &t)
{
    cout << "t.data = " << t.data << endl;
    cout << "Object count = " << Test::count << endl;
}

int main()
{
    Test t1(100);
    Test t2(200);
    fun(t1);
}
View Code

静态方法只能访问静态变量,不能访问普通变量

普通方法技能访问静态变量,也能访问普通变量

为啥会这样呢?

对于类的普通成员函数,该函数必须经由一个对象去激活(有一个this指针)。

但是这条规则对于static修饰的成员函数(静态方法)不适用,静态方法没有this指针。我们代码里面访问data,实际上事以this->data这种方式,而静态函数连this都没有,他怎么能访问data呢?

#include<iostream>
using namespace std;

class Test
{
    friend void fun(Test &t);
public:
    Test(int d=0)
    {
        count++;
        data = d;
    }
    ~Test()
    {
        count--;
    }
    void show1()
    {
        cout << "data = " << data << endl;
        cout << "Object count = " << Test::count << endl;
    }
    static void show2()
    {
        cout << "Object count = " << Test::count << endl;
    }
    void show3()
    {
        show1();
        show2();
    }
    static void show4()
    {
        show2();
    }

private:
    int data;
    static int count;
};

int Test::count = 0;

void fun(Test &t)
{
    cout << "t.data = " << t.data << endl;
    cout << "Object count = " << Test::count << endl;
}

int main()
{
    Test t1(100);
    Test t2(200);
    fun(t1);
}
View Code

静态方法只能访问静态方法,不能访问普通方法

普通方法技能访问静态方法,也能访问普通方法

原因同上

const方法

#include<iostream>
using namespace std;

class Test
{
    friend void fun(Test &t);
public:
    Test(int d=0)
    {
        count++;
        data = d;
    }
    ~Test()
    {
        count--;
    }
    //void change(Test * const this)  导致this为常量
    void change()
    {
        data--;
        cout << endl;
    }
    //void change(Test * const this)const  演变成
    //void change(const Test * const this)  导致*this 和 this都是常量
    void change()const
    {
        //data--;
        cout << endl;
    }

private:
    int data;
    static int count;
};

int Test::count = 0;

void fun(Test &t)
{
    cout << "t.data = " << t.data << endl;
    cout << "Object count = " << Test::count << endl;
}

int main()
{
    Test t1(100);
    Test t2(200);
    fun(t1);
}
View Code

const常方法限定该函数不能修改类的数据成员

#include<iostream>
using namespace std;

class Test
{
    friend void fun(Test &t);
public:
    Test(int d=0)
    {
        count++;
        data = d;
    }
    ~Test()
    {
        count--;
    }
    void show1()
    {
        cout << endl;
    }
    void show2() const
    {
        cout << endl;
    }
    //void change1(Test * const this)  导致this为常量
    void change1()
    {
        show1();
        show2();
    }
    //void change2(Test * const this)const  演变成
    //void change2(const Test * const this)  导致*this 和 this都是常量
    void change2()const
    {
        //show1();
        show2();
    }

private:
    int data;
    static int count;
};

int Test::count = 0;

void fun(Test &t)
{
    cout << "t.data = " << t.data << endl;
    cout << "Object count = " << Test::count << endl;
}

int main()
{
    Test t1(100);
    Test t2(200);
    fun(t1);
}
View Code

普通方法能够调用常方法

常方法不能调用普通方法

posted @ 2018-08-17 21:01  克拉默与矩阵  阅读(179)  评论(0编辑  收藏  举报