It’s quite confusing, but static keyword have 3 different meanings in c++ source code file .cpp and c++ header .hpp, if you ever faced this error
Cannot declare member function static int Foo::bar() to have static linkage
then you are on right place to investigate why this happened.
Inside a function
Inside a function, the static keyword with a variable declaration means that the variable’s value doesn’t disappear when execution exits that function. Each time that function is called, the variable at the start of execution is the same as it was at the previous exit of the function.
Inside a class definition
Inside a class definition, the static keyword means that the variable is attached to the class as opposed to an instance. All instances of that class will simultaneously share that variable.
For global variables
When static is used with a global variable, it means that it is not visible to any code in other files. (Note: files, not classes.)
This use is deprecated. Instead, you should use anonymous namespaces to hide code from other parts of the program.
by url
So this is correct:
class SomeClass
{
public:
static int SomeFunction();
};
int
SomeClass::SomeFunction() { return 0;}
this is NOT correct:
class OtherClass
{
public:
static int OtherFunction();
};
static int
OtherClass::OtherFunction() { return 0;}