C++中类中的静态变量成员与静态成员函数

C++中类中的静态变量成员与静态成员函数

声明为static的类成员(成员数据或成员函数)称为类的静态成员特性 :

  • 静态成员为所有的类对象所共享,不属于某个具体的事例
  • 静态成员即可用类名::静态成员或者对象.静态成员所访问
  • 静态成员变量必须在类外所定义,定义时不能添加static关键字
  • 静态成员函数没有默认的this指针,因为它里面不能使用任何非静态成员
  • 静态成员和普通的类成员一样,也有public protected private 三种访问级别,也可以具有返回值,const修饰符等参数

静态变量成员

静态变量成员的初始化

对于静态变量成员的初始化,不能在构造函数的初始化列表中直接进行初始化

class Date
{
public :
    Date(int y = 1998, int m = 2, int d = 15)
        :year(y)
        ,month(m)
        ,day(d) //直接在初始化列表中对静态变量成员day进行初始化
    {}

    friend ostream& operator<<(const Date&, ostream&);//

private :
    int year;
    int month;
    static int day;
};

//错误    C2438   “day”: 无法通过构造函数初始化静态类数据 7.28    d:\code\7.28\7.28\main.cpp  227 

直接在构造函数的初始化列表中进行操作会发生编译错误
要对static修饰的成员变量进行操作必须在类外先对其进行初始化,初始化的格式为 :(静态变量成员数据类型)(类名):: (变量名) = (要初始数据值)

class Date
{
public :
    Date(int y = 1998, int m = 2, int d = 15)
        :year(y)
        ,month(m)
        //,day(d)
    {}

    friend ostream& operator<<(const Date&, ostream&);//

private :
    int year;
    int month;
    static int day;
};

int Date::day = 14; //初始化

在类外初始化完成之后可以用成员函数或者友元函数对其进行访问与操作

class Date
{
public :
    Date(int y = 1998, int m = 2, int d = 15)
        :year(y)
        ,month(m)
        //,day(d)
    {
        day = d;
    }

    friend ostream& operator<<(const Date&, ostream&);//

private :
    int year;
    int month;
    static int day;
};

int Date::day = 14;

ostream& operator<<(const Date& d, ostream& os)
{
    os << d.year << " " << d.month << " " << d.day;
    return os;
}

静态成员函数

静态成员函数是指以static关键字修饰的类中的成员函数

  • 不可以调用类的非静态成员。
  • 静态成员函数不含this指针。 静态成员函数属于这个类,不再仅仅属于具体的对象。

因此类的静态成员函数和类的普通成员函数的区别是:

  • 静态成员函数不包含指向具体对象的this指针;
  • 普通成员函数包含一个指向具体对象的this指针。

接下来讨论静态成员函数,静态变量成员调用与被调用的关系

例1(静态成员函数调用静态成员变量)

#include <iostream>
using namespace std;

class Date
{
public :
    Date(int y = 1998, int m = 2, int d = 15)
        :year(y)
        ,month(m)
        //,day(d)
    {
        day = 16;
    }

    static void Print()
    {
        cout << "this is the static function ..." << "day :" << day << endl;
    }

    friend ostream& operator<<(const Date&, ostream&);//

private :
    int year;
    int month;
    static int day;
};

int Date::day = 14;

ostream& operator<<(const Date& d, ostream& os)
{
    os << d.year << " " << d.month << " " << d.day;
    return os;
}

int main()
{
    Date d1;
    d1.Print();
    //d1<<cout;
    return 0;
}

运行结果
这里写图片描述
结论 : 静态成员函数可以调用静态成员变量

例2(静态成员变量调用非静态成员变量)

#include <iostream>
using namespace std;

class Date
{
public :
    Date(int y = 1998, int m = 2, int d = 15)
        :year(y)
        ,month(m)
        //,day(d)
    {
        day = 16;
    }

    static void Print()
    {
        cout << "this is the static function ..." << "day :" << day <<"unstatic : "<<year<< endl;
    }

    friend ostream& operator<<(const Date&, ostream&);//

private :
    int year;
    int month;
    static int day;
};

int Date::day = 14;

ostream& operator<<(const Date& d, ostream& os)
{
    os << d.year << " " << d.month << " " << d.day;
    return os;
}

int main()
{
    Date d1;
    d1.Print();
    //d1<<cout;
    return 0;
}

编译结果 :
错误 C2597 对非静态成员“Date::year”的非法引用 7.28d:\code\7.28\7.28\main.cpp 233

结论 : 静态成员函数无法调用非静态成员变量

例3(非静态成员函数调用静态成员变量)

#include <iostream>
using namespace std;

class Date
{
public :
    Date(int y = 1998, int m = 2, int d = 15)
        :year(y)
        ,month(m)
        //,day(d)
    {
        day = 16;
    }

    static void Print()
    {
        cout << "this is the static function ..." << "day :" << day << endl;
    }

    friend ostream& operator<<(const Date&, ostream&);//
    void Add()
    {
        year++;
        month++;
        day++;
    }

private :
    int year;
    int month;
    static int day;
};

int Date::day = 14;

ostream& operator<<(const Date& d, ostream& os)
{
    os << d.year << " " << d.month << " " << d.day;
    return os;
}

int main()
{
    Date d1;
    d1<<cout;
    d1.Add();
    d1 << cout;
    return 0;
}

运行结果 :
这里写图片描述
结论 : 非静态成员可以访问与操作静态成员变量

例4(探测静态成员函数是否含有this指针)

#include <iostream>
using namespace std;

class Date
{
public :
    Date(int y = 1998, int m = 2, int d = 15)
        :year(y)
        ,month(m)
        //,day(d)
    {
        day = 16;
    }

    static void Print()
    {
        //cout << "this is the static function ..." << "day :" << day << endl;
        cout << "this " << this << endl;
    }

    friend ostream& operator<<(ostream&, const Date&);//
    void Add()
    {
        year++;
        month++;
        day++;
    }

private :
    int year;
    int month;
    static int day;
};

int Date::day = 14;

ostream& operator<<(ostream& os,const Date& d)
{
    os << d.year << " " << d.month << " " << d.day;
    return os;
}

int main()
{
    Date d1;
    d1.Print();
    //cout << d1 << endl;
    //d1.Add();
    //cout << d1 << endl;
    return 0;
}

编译结果 :
错误 C2355 “this”: 只能在非静态成员函数或非静态数据成员初始值设定项的内部引用 7.28 d:\code\7.28\7.28\main.cpp 234

结论 : 静态成员函数是没有this指针存在的,所以无法对非静态成员变量进行操作

猜你喜欢

转载自blog.csdn.net/liu_zhen_kai/article/details/81272136