当全局命名空间中的函数声明为静态C ++时,是什么意思? [英] What does it mean when the function in global namespace is declared as static C++?

查看:144
本文介绍了当全局命名空间中的函数声明为静态C ++时,是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

什么是静态函数?

我在全局命名空间中看到一个函数, this:

I have seen a function in a global namespace that is declared like this:

static int function_name(int a, double* p, int c, float * u)
{
     //do something with these arguments
}

编辑:现在当我知道什么是静态时,请解释什么优点给了一个函数的限制在文件只有声明它是可见的?

Now when I know what is for static, please explain what advantage gives the restriction of a function to be visible in a file only where it is declared? I mean why I should restrict my function visibility, what it gives to me?

推荐答案

您将使用范围的静态函数在编译单元中,当你真的想要该名称的函数只在编译单元内部是已知的。不在该范围内的类或函数不能意外调用该函数。 (我会把这个在评论,但我没有那个权限)

You would use a static function with the scope in the compilation unit when you really want the function with that name to be known only inside that compilation unit. A class or function that is not in that scope cannot accidentally call the function. (I'd have put this in a comment but I don't have privileges for that yet)

这篇关于当全局命名空间中的函数声明为静态C ++时,是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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