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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 13:55:13  来源:igfitidea点击:

Member function with static linkage

c++static

提问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 statichas 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, staticmeans "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, staticmeans "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 statichere 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 staticto 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 statickeyword from the definition:

幸运的是,正如您所指出的,有一个简单的解决方法:只需static从定义中删除关键字:

void Foobar::do_something() {} // Should be good to go!

This is perfectly fine because the compiler already knows that do_somethingis a staticmember function, since you told it about that earlier on.

这完全没问题,因为编译器已经知道这do_something是一个static成员函数,因为你之前告诉过它。

Hope this helps!

希望这可以帮助!