今天在写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最新的书<<The C++ programming language>> 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++中静态函数调用方式的变化,介绍了在新标准下,不仅可以通过类名直接调用静态函数,还可以通过类的对象进行调用,提供了具体的代码示例,并引用了Bjarne Stroustrup的著作《C++编程语言》第四版中的相关内容。
1万+





