Something about static Methods in C++ you should know

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.


你可能感兴趣的:(static)