Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is a static member function _declared_ static, but _defined_ otherwise in C++?

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?

like image 983
John Doucette Avatar asked Nov 23 '12 14:11

John Doucette


People also ask

Why are function declared as static in C?

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.

Why we can not declare static member function constant or virtual?

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.

Why do we declare a function as static?

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.

What happens if we declare a member function of a class as static?

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 ::.


1 Answers

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.

like image 102
J.N. Avatar answered Sep 23 '22 15:09

J.N.