开发者

Usage of static keyword in C++

开发者 https://www.devze.com 2023-02-06 04:12 出处:网络
I have a class exposing a static function in myclass.hpp class MyClass { public: static std::string dosome();

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 My开发者_JS百科Class::dosome() {
   ...
}

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


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.


Do not repeat the static keyword. To do so will result in an error.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消