你应该把全局常量放在C ++程序中? [英] Where should you put global constants in a C++ program?

查看:145
本文介绍了你应该把全局常量放在C ++程序中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++应用程序中将全局常量放在哪里?例如你会把他们在一个类吗?在结构中?

Where would you put global constants in a C++ application? For example would you put them in a class? In a struct?

推荐答案

我将使用一个命名空间作为与单个类非常相关的全局常量。在后一种情况下,我会把它们放在那个类中。

I would use a namespace for global constants which are not very strongly associated with a single class. In the latter case, I would put them in that class.

真的全局(应用程序级别)常量应该在应用程序的命名空间只要你的应用程序在它自己的命名空间内,因为它应该是)。对于模块级常量,模块自己的命名空间是自然的地方。

Really global (application-level) constants should be in the application's namespace (provided your application is inside it's own namespace, as it should be). For module-level constants, the module's own namespace is the natural place.

这篇关于你应该把全局常量放在C ++程序中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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