使全局“常量"的正确方法在 C++ 中

编程入门 行业动态 更新时间:2024-10-28 00:27:38
本文介绍了使全局“常量"的正确方法在 C++ 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

通常,我定义一个真正的全局常量(比如 pi)的方法是在头文件中放置一个 extern const,并在 .cpp 文件中定义常量:

常量.h:

extern const pi;

常量.cpp:

#include "constants.h"#include <cmath>const pi=std::acos(-1.0);

这对真正的常量如 pi 非常有用.但是,当涉及到定义常量"时,我正在寻找最佳实践,因为它在程序运行到程序运行之间保持不变,但可能会根据输入文件而改变.这方面的一个例子是重力常数,它取决于所使用的单位.g 在输入文件中定义,我希望它是任何对象都可以使用的全局值.我一直听说使用非常量全局变量是不好的做法,所以目前我将 g 存储在系统对象中,然后将其传递给它生成的所有对象.但是,随着对象数量的增加,这似乎有点笨拙且难以维护.

想法?

解决方案

这完全取决于您的应用程序大小.如果您真的绝对确定某个特定常量在一次运行中将具有由代码中的所有线程和分支共享的单个值,并且将来不太可能改变,那么全局变量最接近预期的语义,所以最好只使用它.如果需要,稍后重构也很简单,特别是如果您为全局变量使用独特的前缀(例如 g_),以便它们永远不会与本地变量发生冲突 - 这通常是一个好主意.

总的来说,我更喜欢坚持使用YAGNI,不要盲目安抚各种编码风格指南.相反,我首先查看他们的基本原理是否适用于特定情况(如果编码风格指南没有基本原理,那么它是一个糟糕的),如果显然没有,那么就没有理由应用该指南到那种情况.

Typically, the way I'd define a true global constant (lets say, pi) would be to place an extern const in a header file, and define the constant in a .cpp file:

constants.h:

extern const pi;

constants.cpp:

#include "constants.h"
#include <cmath>
const pi=std::acos(-1.0);

This works great for true constants such as pi. However, I am looking for a best practice when it comes to defining a "constant" in that it will remain constant from program run to program run, but may change, depending on an input file. An example of this would be the gravitational constant, which is dependent on the units used. g is defined in the input file, and I would like it to be a global value that any object can use. I've always heard it is bad practice to have non-constant globals, so currently I have g stored in a system object, which is then passed on to all of the objects it generates. However this seems a bit clunky and hard to maintain as the number of objects grow.

Thoughts?

解决方案

It all depends on your application size. If you are truly absolutely sure that a particular constant will have a single value shared by all threads and branches in your code for a single run, and that is unlikely to change in the future, then a global variable matches the intended semantics most closely, so it's best to just use that. It's also something that's trivial to refactor later on if needed, especially if you use distinctive prefixes for globals (such as g_) so that they never clash with locals - which is a good idea in general.

In general, I prefer to stick to YAGNI, and don't try to blindly placate various coding style guides. Instead, I first look if their rationale applies to a particular case (if a coding style guide doesn't have a rationale, it is a bad one), and if it clearly doesn't, then there is no reason to apply that guide to that case.

这篇关于使全局“常量"的正确方法在 C++ 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-28 06:43:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1169637.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:常量   全局   正确   方法   quot

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!