Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/133.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在C++;_C++_Static Methods - Fatal编程技术网

C++ 在C++;

C++ 在C++;,c++,static-methods,C++,Static Methods,我有一个类在myclass.hpp中公开了一个静态函数 class MyClass { public: static std::string dosome(); }; 那么,在myclass.cpp中,我应该写什么: 这: 或者这个: static std::string MyClass::dosome() { ... } 我想我不应该重复静态关键字。。。是否正确?不要重复静态关键字。否则将导致错误。C++编译器不允许这样做: static std::string MyClass

我有一个类在myclass.hpp中公开了一个静态函数

class MyClass {
public:
   static std::string dosome();
};
那么,在myclass.cpp中,我应该写什么: 这:

或者这个:

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

我想我不应该重复静态关键字。。。是否正确?

不要重复
静态
关键字。否则将导致错误。

C++编译器不允许这样做:

static std::string MyClass::dosome() {
   ...
}
因为在函数定义中具有
静态
意味着完全不同的东西-
静态
链接(意味着只能从同一翻译单元调用函数)


在成员函数声明中有
静态
就足够了。

。在类定义之外定义函数体时,不应使用
static
关键字。

不要重复static关键字,但我很想知道why@MattSmith字体见下面我的答案。啊,是的,你是对的。。。就像是。。。如果它遇到一个静态函数,这意味着它只能从同一个函数调用。。。文件或更好,正如你所指出的,翻译单位。谢谢你的夸大其词的回答。
static std::string MyClass::dosome() {
   ...
}