Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

the position of the *static* keyword in member method declaration

Tags:

c++

Is there any difference between

class C {
    static int func();
};

and

class C {
    int static func();
};

I'm trying to remove the keyword static in someone else's code base. And I want to make sure I understand what the second example means before I do that.

[Edit] The reason to remove static: C was a "class" with no member variables and full of static methods. I think it's more proper to make "C" a namespace with normal functions instead of a class.

like image 624
GuLearn Avatar asked Apr 19 '13 14:04

GuLearn


2 Answers

There is no difference. static on the function declaration applies to the function.
An this pointer will not be implicitly passed to this function, So you cannot access non static class members inside this function without explicitly passing the object to it.

To remove the static first you should know and understand the purpose that it is designed this way. Without taking that in to consideration you are just bound to create a code smell of vast proportions.

like image 75
Alok Save Avatar answered Nov 17 '22 16:11

Alok Save


Yes, in the first case, the keyword static comes before the type int, in the second, they are the other way around. However, like many things in C and C++, there is no semantic difference. So, other than "cosmetically", there is no difference.

I'm not sure why you would want to remove static in classes as a general rule - there is probably a good reason a member function is declared static.

like image 36
Mats Petersson Avatar answered Nov 17 '22 15:11

Mats Petersson