c++调用静态函数的方法

        今天在写c++程序时,发新现在的gcc和vc++对于静态函数的调用方式有所改变,在旧标准的c++中,调用类的静态方法只能用类名::函数名的方式,在新的标准中,类的对像也可以像调用类的方法一样调用静态函数。示例如下:

class Date{
    int y,m,d;
public:

    Date();

    static void Print();
};

Date::Date()

{}

void Date::Print()
{
    printf("Hello World\n");
}

调用方法,.3种都行:

    Date::Print();

    Date* date = new Date();
    date->Print();

    Date obj;
    obj.Print();

于是查阅了Bjarne Stroustrup最新的书<> Fourth edition,16.2.12 [static] Members

有这么一句话:A static member can be referred to like any other member. In addition,a static member can be referred to without memtioning an object. Instead,its name is qualified by the name of its class.

也就是说,静态成员可以像其它成员一样引用,另外,静态成员也可以不通过类对象引用,而是前面加上它的类的名称限定的方式来引用。

你可能感兴趣的:(c++,c++,static,c++调用静态函数)