1.
It's not allowed to declare staticmethods as const.
The non-static versions of the methods can be marked as const.
2.
The static methods are scoped by the name of the class in which they are defined, but are notmethods that apply to a specific object.
In C++,you cannot override a static method.
First of all,a method cannot be both static and virtual.
If you have a staticmethod in your subclass with the same name as a static method in your superclass, you actuallyhave two separate methods. These two methods are in no way related.
class SuperStatic
{
public:
static void beStatic()
{
cout << "SuperStatic being static." << endl;
}
};
class SubStatic : public SuperStatic
{
public:
static void beStatic()
{
cout << "SubStatic keepin' it static." << endl;
}
};
SuperStatic::beStatic();
SubStatic::beStatic();
The results:
SuperStatic being static.
SubStatic keepin' it static.
SubStatic mySubStatic;
SuperStatic& ref = mySubStatic;
mySubStatic.beStatic();
ref.beStatic();
The results:
SubStatic keepin' it static.
SuperStatic being static.
本文探讨了C++中静态方法的特点与限制,包括不能同时声明为static和const,以及静态方法不能被重写等特性,并通过示例代码展示了静态方法在继承中的行为表现。
3368

被折叠的 条评论
为什么被折叠?



