Using a static keyword in C ++

I have a class displaying 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 that:

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

I think I should not repeat the static keyword ... is this correct?

+4
source share
3 answers

The C ++ compiler does not allow this:

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

since static in a function definition means something completely different - static linkage (which means that a function can only be called from one translation unit).

Having static in a member function declaration is sufficient.

+10
source

Do not repeat the static . An error will occur for this.

+5
source

Yes The static should not be used when defining a function body outside the class definition.

+1
source

Source: https://habr.com/ru/post/1336772/


All Articles