Why can static member function definitions not have the keyword 'static'?

后端 未结 4 1176
北海茫月
北海茫月 2021-01-01 15:28

As per this link on the \'static\' keyword in C++ :

The static keyword is only used with the declaration of a static member, inside the class defini

相关标签:
4条回答
  • 2021-01-01 15:40

    You have to understand the difference between declaration and implementation, and that will answer your question:

    Declaration: Is how C++ functions and methods are seen before compiling the program. It's put in a header file (.h file).

    Implementation: Is how the compiler links a declaration to a real task in binary code. The implementation can be compiled on the fly (from source files, .cpp or .cxx or .cc), or can be already compiled (from shared libraries or object files).

    Now going back to your question, when you declare something as static, it's something not related to the implementation, but related to how the compiler sees the decleration while compiling the code. For example, if you label functions in source files "static", then that's meaningless, because that information cannot be carried to compiled objects and shared libraries. Why allow it? On the contrary, it could only cause ambiguity.

    For the exact same reason, default parameters must go into the header, not the source files. Because source files (that contain implementations), cannot carry the default parameter information to a compiled object.

    0 讨论(0)
  • 2021-01-01 15:44

    There's ambiguity alright. The same definition need not be for a member function at all.

    Consider this:

    namespace foo {
        static void bar();
    }
    
    static void foo::bar() {
    
    }
    

    foo::bar is required to be defined with the same linkage specifier.

    For member functions, however, static is not a linkage specifier. If it was allowed, the correctness of the definition of foo::bar will be very very context dependent on what foo is. Disallowing static in fact eases the burden on the compiler.

    Extending it to members in general, as opposed to just member functions, is a matter of consistency.

    0 讨论(0)
  • 2021-01-01 15:45

    The point is, that static has several, very different meanings:

    class Foo {
        static void bar();
    }
    

    Here the static keyword means that the function bar is associated with the class Foo, but it is not called on an instance of Foo. This meaning of static is strongly connected to object orientation. However, the declaration

    static void bar();
    

    means something very different: It means that bar is only visible in file scope, the function cannot be called directly from other compilation units.

    You see, if you say static in the class declaration, it does not make any sense to later restrict the function to file scope. And if you have a static function (with file scope), it does not make sense to publish it as part of a class definition in a public header file. The two meanings are so different, that they practically exclude each other.


    static has even more, distinct meanings:

    void bar() {
        static int hiddenGlobal = 42;
    }
    

    is another meaning, that is similar, but not identical to

    class Foo {
        static int classGlobal = 6*7;
    }
    

    When programming, words don't always the same meaning in all contexts.

    0 讨论(0)
  • 2021-01-01 15:52

    Wild guess but if the definition has static it could be interpreted as a file-scope variable in the C sense.

    0 讨论(0)
提交回复
热议问题