在全局范围内使用静态变量和函数 [英] Use of static variables and functions in global scope

查看:114
本文介绍了在全局范围内使用静态变量和函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当它位于.cpp文件的全局范围内,而不是在函数中时,是否可以将变量标记为 static

Is there a use for flagging a variable as static, when it lies in the global scope of a .cpp file, not in a function?

你可以使用static关键字的函数吗?如果是,它们的用途是什么?

Can you use the static keyword for functions as well? If yes, what is their use?

推荐答案

在这种情况下,关键字static表示函数或变量只能由代码使用在同一个cpp文件中。

In this case, keyword static means the function or variable can only be used by code in the same cpp file. The associated symbol will not be exported and won't be usable by other modules.

这是避免在大软件中发生名称冲突的好习惯,当你知道你的全局函数时,其他模块中不需要变量。

This is good practice to avoid name clashing in big software when you know your global functions or variables are not needed in other modules.

这篇关于在全局范围内使用静态变量和函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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