C++ 类外的静态函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25724787/
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
Static functions outside classes
提问by cuv
Could someone tell me what's the purpose of declaring static functions outside classes? What's the difference between this 2? Are there any benefits for using static in this situation?
有人能告诉我在类外声明静态函数的目的是什么吗?这2个有什么区别?在这种情况下使用静态有什么好处吗?
static void someRandomFunction();
int main()
{
someRandomFunction();
return 0;
}
and
和
void someRandomFunction();
int main()
{
someRandomFunction();
return 0;
}
回答by Mike Seymour
At namespace scope, static
gives a name internal linkage, meaning that it is only accessible within the translation unit that contains the definition. Without static
, it has external linkage, and is accessible in any translation unit.
在命名空间范围内,static
给出一个名称internal links,这意味着它只能在包含定义的翻译单元内访问。没有static
,它具有外部链接,并且可以在任何翻译单元中访问。
So you'd use static
(or, alternatively, an unnamed namespace) when writing a function that's only intended for use within this unit; the internal linkage means that other units can define different functions with the same name without causing naming conflicts.
因此static
,在编写仅打算在此单元中使用的函数时,您将使用(或者,一个未命名的命名空间);内部联动意味着其他单元可以定义同名的不同功能,而不会造成命名冲突。
Non-static functions (and global names in general) are better declared in a header, to make sure that every translation unit that uses them gets the same declaration.
非静态函数(以及一般的全局名称)最好在标头中声明,以确保使用它们的每个翻译单元都获得相同的声明。
回答by Some programmer dude
The static
keyword on global functions or variables limits the visibility and linkage scope of the function or variable to the current translation unit.
static
全局函数或变量上的关键字将函数或变量的可见性和链接范围限制到当前翻译单元。
That means that for a function, it can only be called from the current source file, and not from other source files.
这意味着对于一个函数,它只能从当前源文件中调用,而不能从其他源文件中调用。
回答by Adi Shavit
A static
function remains visible only in file scope. This is a C feature.
The recommended way to do it in C++ is using an anonymous namespace, as in:
一个static
函数只在文件范围内仍然可见。这是一个 C 特性。
在 C++ 中推荐的方法是使用匿名命名空间,如下所示:
namespace // no name, i.e. anonymous
{
void someRandomFunction();
}
int main()
{
someRandomFunction(); // visible only within this file.
return 0;
}
Note that the function bodyalso has to be declared somewhere within the samefile since the linker will not try to find it in other (external) translation units.
So void someRandomFunction();
is really a forward declarationfor a function that is defined elsewhere in the same file (i.e. in the same translation unit).
请注意,函数体也必须在同一文件中的某处声明,因为链接器不会尝试在其他(外部)翻译单元中找到它。
所以void someRandomFunction();
确实是一个向前声明对于在同一个文件(即在相同的翻译单元)别处定义的功能。
If the function is actually called, you will get a linking error unless the function body is defined in the same file.
如果函数被实际调用,除非函数体定义在同一个文件中,否则你会得到一个链接错误。
(The more pedantic technical term is actually not filebut translation-unitsince the body might be in an #include
ed header do not in the actual file per-se. )
(更迂腐的技术术语实际上不是文件,但翻译单元,因为身体可能处于#include
ED头在实际的文件不要每本身。)
回答by Adam
Static methods and static functions are entirely different things.
静态方法和静态函数是完全不同的东西。
Static methods are methods of a class instead of an instance (which you already know, as it seems).
静态方法是类的方法,而不是实例(您似乎已经知道)。
Static functions, on the other hand, are function which are available only in the module they are defined in. They are not exported and cannot be put in a header file and used in another c file. This way you can write different functions sharing the same name, and also the compiler may optimize your code more thoroughly by inlining the function, knowing that no other file is dependant on it.
另一方面,静态函数是仅在定义它们的模块中可用的函数。它们不能导出,也不能放在头文件中并在另一个 c 文件中使用。通过这种方式,您可以编写共享相同名称的不同函数,并且编译器可以通过内联函数来更彻底地优化您的代码,因为知道没有其他文件依赖于它。
回答by Dr. Debasish Jana
static void someRandomFunction();
This has to be used within same compilation unit (source file) and outside that compilation unit, not available for use. Whereas, if you have
这必须在同一编译单元(源文件)内和该编译单元外使用,不可使用。然而,如果你有
void someRandomFunction();
with one definition acrosss the program, the function can be used by any compilation unit globally across the program
通过跨程序的一个定义,该函数可以被程序中全局的任何编译单元使用
回答by doron
static
tells the compiler not add the function to the symbol table for the object file. This effectively means that the link is unable to find the function which in turn means you can only use the function directly in the current compilation unit. You can however call static functions from another compilation unit if this is done through a function pointer.
static
告诉编译器不要将函数添加到目标文件的符号表中。这实际上意味着链接无法找到该函数,这反过来意味着您只能直接在当前编译单元中使用该函数。但是,如果这是通过函数指针完成的,您可以从另一个编译单元调用静态函数。