Here's a minimal working example:
A.h:
class A{
static int a_member_function();
};
A.cpp
#include "A.h"
int A::a_member_function(){return 5;}
int main(){ return 1;}
This code compiles and runs, but, it seems to me that:
static int A::a_member_function(){return 5;}
could just as easily be used to define the static member function of class A. Indeed, it seems like it could actually be rather useful to have this requirement, since it would remind the reader of the .cpp file that a_member_function is static.
However, this clearly doesn't work:
error: cannot declare member function ‘static int A::a_member_function()’ to have static linkage [-fpermissive]
So why doesn't it work? What's the reasoning behind this decision?
A static function in C is a function that has a scope that is limited to its object file. This means that the static function is only visible in its object file. A function can be declared as static function by placing the static keyword before the function name.
A 'const member function' is not allowed to modify the object it is called on, but static member functions are not called on any object. It is used directly by scope resolution operator. Thus having a const static member function makes no sense, hence it is illegal.
Unlike global functions in C, access to static functions is restricted to the file where they are declared. Therefore, when we want to restrict access to functions, we make them static. Another reason for making functions static can be reuse of the same function name in other files.
By declaring a function member as static, you make it independent of any particular object of the class. A static member function can be called even if no objects of the class exist and the static functions are accessed using only the class name and the scope resolution operator ::.
The reason behind is C++ spirit to try and minimize the number of its keywords and keep backward compatibility with C: static
in that position has an entirely different meaning.
It all predates back to C. "static" functions in C are functions that are unique to a compilation unit (a .c file). They can't be accessed by other compilation units (it's one way to have encapsulation in C). This usage is still valid in C++. You can also do the same for global variables to limit their scope.
Though, in C++ you also want to declare member functions as static
for a different reason: those functions belong to class but do not require and instance of said class to run (I bet you know that already, I am just trying to be complete).
Defining a member function as static
would lead to a contradiction: that function must be accessed outside of its translation unit.
There's another case of keyword reuse between C and C++, the auto
keyword in C++11, but it is less likely to be a problem.
Note:
The same thing happens with the virtual
keyword which is present in the declaration and not the definition.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With