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 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.
精彩评论