How to Declare and Define a Static Member Function in C++?
class CLASS_NAME
{
static int func1(args) // (a) Definition. You must use the static keyword here.
{
...
};
static int func2(args); // (b) Declaration. You must use the static keyword here.
};
int CLASS_NAME::func2(args) // (c) Definition. You must not use the static keyword here.
{
...
};
Static member functions must use the static keyword inside the class definition (see (a) and (b) above).
Static member functions must not use the static keyword outside the class definition (see (c) above). If you do, you'll get a compilation warning or error similar to “warning: cannot declare member function 'static ...' to have static linkage”.
FYI: Static member functions cannot be constant member functions. i.e. they cannot have the keyword const after the declaration or definition.
warning/error: cannot declare member function 'static ...' to have static linkage:
Static member functions must not use the static keyword outside the class definition (see (c) above).
Tags: warning cannot declare member function to have static linkage, error cannot declare member function to have static linkage, static member function, static class function, C++