Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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 definition, but not with the definition of that static member.

Why is the static keyword prohibited on member function definitions? I do understand that re-declaring a function as 'static' at its definition is redundant. But using it should be harmless during compilation of the function definition as it does not result in any kind of ambiguity. So why do compilers prohibit it?

like image 687
johngreen Avatar asked Jul 24 '17 10:07

johngreen


People also ask

Why this keyword is not applicable in static member functions?

Which among the following is not applicable for the static member functions? Explanation: Since the static members are not property of objects, they doesn't have this pointer. Every time the same member is referred from all the objects, hence use of this pointer is of no use.

Can a member function be static?

Static Function Members A static member function can only access static data member, other static member functions and any other functions from outside the class. Static member functions have a class scope and they do not have access to the this pointer of the class.

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.

Is static function and static member function same?

You cannot have static and nonstatic member functions with the same names and the same number and type of arguments. Like static data members, you may access a static member function f() of a class A without using an object of class A .


2 Answers

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.

like image 199
StoryTeller - Unslander Monica Avatar answered Oct 01 '22 02:10

StoryTeller - Unslander Monica


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.

like image 41
cmaster - reinstate monica Avatar answered Oct 01 '22 03:10

cmaster - reinstate monica