Something about static Methods in C++ you should know

来源:互联网 发布:图片美化软件 编辑:程序博客网 时间:2024/05/17 04:53
1.
It's not allowed to declare static methods 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 not methods 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 static method in your subclass with the same name as a static method in your superclass, you actually have 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.


原创粉丝点击