C++将 const 分配给环境变量或默认值
对于使用多个环境变量的应用程序,在获取环境变量并将其放入结构体或一堆 const 时是否存在某种约定或“最佳实践”?是?显然,我想为每个环境变量回退到默认值。现在,使用以下方法似乎是一种非常混乱的方法:
char* x;
const SOME_VARIABLE;
if (NULL == (x = getenv("SOME_VARIABLE")))
SOME_VARIABLE = 5; // default value
else
SOME_VARIABLE = x;
我还可以编写一个包装 getenv 的函数,以在环境变量为空时返回默认值,但我不确定如果这是最好的方法的话。我也可以放弃使用 const,但这似乎也不是一件好事。
有什么想法吗?
For an application that uses a number of environment variables, is there some kind of a convention or "best practice" when it comes to grabbing environment variables and putting them into either a struct or a bunch of const
's? Obviously, I want to fallback to a default value for each and every environment variable. Right now, using the following seems like a very messy way of doing it:
char* x;
const SOME_VARIABLE;
if (NULL == (x = getenv("SOME_VARIABLE")))
SOME_VARIABLE = 5; // default value
else
SOME_VARIABLE = x;
I could also write a function that wraps getenv
to return a default value if an environment variable is empty, but I'm not sure if that's even the best way to do it. I could also do away with using const
, but that doesn't seem like a good thing to do, either.
Any thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
怎么样:
用作:
How about:
Used as:
James McNellis 提供了很好的答案,但这里有一个替代意见:
我认为
getenv()
返回值程序启动时的环境变量。如果程序启动后环境变量发生更改,getenv()
仍将返回旧值。基于此,另一种方法可能是使用一个类来捕获所有必需的环境变量作为属性。该类在程序开始时填充,并且仅提供(const 限定的)访问器方法。这样,每个环境变量都会恰好调用一次getenv()
。不利的一面是,班级占用了一些空间。在不使用任何空间的替代方法中,每当需要任何环境变量的值时都会调用 getenv() 。这并不是说 getenv() 调用很昂贵(实际上我不知道),而是它给程序员带来了一个错误的假设,即可能返回最新值。
如果使用环境变量存在任何依赖性,那么拥有一个类也可能有助于抽象,例如
,现在,一个不关心
$OS
但只需要$GCC
,可以参考envClass->get("GCC");
只是一个想法。
James McNellis has provided a great answer, but here is an alternate opinion:
I think
getenv()
returns the value of the environment variable at the time the program was started. If the environment variable gets changed after the program is started,getenv()
would still return the old value. Based on that, an alternate approach may be to have a class to capture all the required environment variables as attributes. The class is populated at the start of the program, and provides only (const-qualified) accessor methods. That way thegetenv()
is called exactly once for each environment variable. On the negative side, the class takes up some space.In the alternate approach, which does not use any space,
getenv()
is called whenever the value of any environment variable is required. It’s not that thegetenv()
call is expensive (actually I don’t’ know), but it carries a false assumption to the programmer that the latest value might be returned.Also having a class may be useful to abstract if there is any dependency in using the environement variables, e.g.
Now, a function who don’t care about
$OS
but needs only$GCC
, can just refer toenvClass->get("GCC");
Just a thought.
我通常使用加载配置设置(从环境或配置文件)的类来执行此操作。我在启动时实例化它的单个全局实例。
程序设置是配置类对象的方法或属性。
这很简单,当你编写代码时,你的意图就很清楚了:
I usually do this with a class that loads configuration settings (either from the environment or a configuration file). I instantiate a single global instance of it at startup.
The program settings are a method or property of the configuration class object.
It's simple and when you write code it's pretty clear what you intended: