问题描述
在文件中使用静态函数的效用是什么?
What is the utility of having static functions in a file ?
它们与文件中的全局函数有什么不同?
How are they different from having global functions in a file ?
static int Square(int i)
{
return i * i;
}
vs
int Square(int i)
{
return i * i;
}
推荐答案
您可以使用这些函数为其他函数提供共享的实现逻辑同一个文件。
You can use these functions to provide shared implementation logic to other functions within the same file. Various helper functions specific to a file are good candidates to be declared file-static.
它们对于链接器是不可见的,允许其他编译单元定义具有相同签名的函数。使用命名空间在很大程度上缓解了这个问题,但是文件 - static
函数早于命名空间,因为它们是继承自C编程语言的特性。
They are invisible to the linker, allowing other compilation units to define functions with the same signature. Using namespaces alleviates this problem to a large degree, but file-static
functions predate namespaces, because they are a feature inherited from the C programming language.
这篇关于C ++静态局部函数与全局函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!