Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Usage of static keyword in C++

I have a class exposing a static function in myclass.hpp

class MyClass {
public:
   static std::string dosome();
};

Well, in myclass.cpp what should I write: this:

std::string MyClass::dosome() {
   ...
}

or this:

static std::string MyClass::dosome() {
   ...
}

I guess I should not repeat the static keyword... is it correct?

like image 331
Andry Avatar asked Dec 05 '22 23:12

Andry


1 Answers

C++ compiler will not allow this:

static std::string MyClass::dosome() {
   ...
}

since having static in a function definition means something completely different - static linkage (meaning the function can only be called from the same translation unit).

Having static in a member function declaration is enough.

like image 167
sharptooth Avatar answered Dec 09 '22 14:12

sharptooth