C ++静态局部函数与全局函数 [英] C++ static local function vs global function

查看:402
本文介绍了C ++静态局部函数与全局函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在文件中使用静态函数的效用是什么?

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;
}


推荐答案


在文件中使用静态函数的效用是什么?

What is the utility of having static functions in a file?

您可以使用这些函数为其他函数提供共享的实现逻辑同一个文件。

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.


它们与文件中具有全局函数有什么不同? ?

How are they different from having global functions in a file?

它们对于链接器是不可见的,允许其他编译单元定义具有相同签名的函数。使用命名空间在很大程度上缓解了这个问题,但是文件 - 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 ++静态局部函数与全局函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆