C++ 具有静态链接的成员函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31305717/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Member function with static linkage
提问by Achal Dave
I'm trying to understand why the following is an error:
我试图理解为什么以下是一个错误:
class Foobar {
public:
static void do_something();
};
static void Foobar::do_something() {} // Error!
int main() {
Foobar::do_something();
}
This errors with "error: cannot declare member function 'static void Foobar::do_something()' to have static linkage" in g++, and "error: 'static' can only be specified inside the class definition" in clang++.
此错误与“错误:无法将成员函数 'static void Foobar::do_something()' 声明为具有静态链接”在 g++ 中,以及“错误:'static' 只能在类定义中指定”在 clang++ 中。
I understand that the way to fix this is to remove "static" in the definition of do_something on line 6. I don't, however, understand why this is an issue. Is it a mundane reason, such as "the C++ grammar dictates so", or is something more complicated going on?
我知道解决这个问题的方法是删除第 6 行 do_something 定义中的“静态”。但是,我不明白为什么这是一个问题。这是一个平凡的原因,例如“C++ 语法规定如此”,还是发生了更复杂的事情?
回答by templatetypedef
I think the issue here is that the keyword static
has several different meanings in C++ and the code you've written above uses them in two different ways.
我认为这里的问题是关键字static
在 C++ 中有几种不同的含义,并且您在上面编写的代码以两种不同的方式使用它们。
In the context of member functions, static
means "this member function does not have a receiver object. It's basically a normal function that's nested inside of the scope of the class."
在成员函数的上下文中,static
意味着“这个成员函数没有接收者对象。它基本上是一个嵌套在类范围内的普通函数。”
In the context of function declarations, static
means "this function is scoped only to this file and can't be called from other places."
在函数声明的上下文中,static
表示“此函数的作用域仅限于此文件,不能从其他地方调用。”
When you implemented the function by writing
当您通过编写实现该功能时
static void Foobar::do_something() {} // Error!
the compiler interpreted the static
here to mean "I'm implementing this member function, and I want to make that function local just to this file." That's not allowed in C++ because it causes some confusion: if multiple different files all defined their own implementation of a member function and then declared them static
to avoid collisions at linking, calling the same member function from different places would result in different behavior!
编译器将static
这里解释为“我正在实现这个成员函数,我想让这个函数只在这个文件中本地化”。这在 C++ 中是不允许的,因为它会引起一些混乱:如果多个不同的文件都定义了自己的成员函数实现,然后声明它们static
以避免链接时发生冲突,那么从不同的地方调用相同的成员函数会导致不同的行为!
Fortunately, as you noted, there's an easy fix: just delete the static
keyword from the definition:
幸运的是,正如您所指出的,有一个简单的解决方法:只需static
从定义中删除关键字:
void Foobar::do_something() {} // Should be good to go!
This is perfectly fine because the compiler already knows that do_something
is a static
member function, since you told it about that earlier on.
这完全没问题,因为编译器已经知道这do_something
是一个static
成员函数,因为你之前告诉过它。
Hope this helps!
希望这可以帮助!