Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't use "static" keyword on a static method in a c++ class implementation file (.cpp)

Consider:

// In Vector2.h

class Vector2
{
    public:
        // returns the degrees in radians
        static double calcDir(double x, double y);
}

// In Vector2.cpp

double Vector2::calcDir(double x, double y)
{
    double rad = ...;
    return rad;
}

Why isn't the keyword static required in the signature in Vector2.cpp? When I try this, it produces an error:

static double Vector2::calcDir(double x, double y)

It seems inconsistent to me. All other parts of the method signature are required to be repeated in the .cpp file (return type, method name (duh), names and types of args, const-ness). I don't like not knowing at a glance whether a method is static or not (when looking at the implementation).

Is there a reason this is not only not required, but forbidden?

like image 636
Samuel Meacham Avatar asked Jan 11 '12 20:01

Samuel Meacham


People also ask

Can a class be static in CPP?

There is no such thing as a static class in C++. The closest approximation is a class that only contains static data members and static methods.

What is a static function in a C++ class?

A static function is a member function of a class that can be called even when an object of the class is not initialized. A static function cannot access any variable of its class except for static variables. The 'this' pointer points to the object that invokes the function.

What is static mean in C++?

Static is a keyword in C++ used to give special characteristics to an element. Static elements are allocated storage only once in a program lifetime in static storage area. And they have a scope till the program lifetime. Static Keyword can be used with following, Static variable in functions.


3 Answers

It's because static has a special meaning when used in a class definition. In a class definition it identifies the function as a static member function which means it doesn't operate on a class instance but can be called independently.

Outside of a class static gives a function internal linkage but this is illegal on (even static) member functions because class members must have the same linkage as the class of which they are a member (almost always external, and certainly external in cases where you can defined member functions outside of the class definition).

From a language point of view, the declaration of members inside a class definition follows one set of language rules where static has its special class meaning. Outside of a class definition, all function definitions - members and non-members - follow the same set of rules where static has it's other meaning which is not valid for members of classes with external linkage.

like image 55
CB Bailey Avatar answered Oct 10 '22 12:10

CB Bailey


This is probably to reduce confusion. At file scope, 'the keyword static is (confusingly) used to mean "use internal linkage" (TC++PL p.200). It could imply that the function, though a member of a class, was only visible inside the current translation unit. Allowing the static specifier there would be more confusing.

Note that using static to denote internal linkage is no longer recommended, and that anonymous namespaces should be preferred to achieve that.

like image 42
Rob K Avatar answered Oct 10 '22 10:10

Rob K


It is annoying that keyword wound up invoking two very different functionalities (global-to-all-class-instances vs. local-to-file) but the following small header may be of use. The macro just expands to an empty string meaning you can tack it in front of the declarations of methods in the implementation file and coming back to your code a year later you'll avoid pouring through a 2000-line "boost-esque" header to find out why the compiler won't let you use this.

staticmethod.h:

/*
** macro sugar for maintaining readability
** of static methods in implementation files
*/
#ifndef STATICMETHOD
#define STATICMETHOD

In your .cpp files you could then use:

#include "staticmethod.h"
STATICMETHOD double Vector2::calcDir(double x, double y)
{
    double rad = ...;
    return rad;
}
like image 44
Brian Jack Avatar answered Oct 10 '22 11:10

Brian Jack