C++ 在运行时分配一个常量值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14131855/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ Assign a const value at run-time?
提问by Casey
I have a constant value that never changes during run-time, but is impossible to know until run-time.
我有一个在运行时永远不会改变的常数值,但直到运行时才知道。
Is there a way to declare a constant (either as a member of a class or not) without defining it and also assign a computed value once (and only once) it is determined; or am I going to have to resort to a non-const declaration and use coding S & Ps (ALL_CAPS
variables names, static
declaration if in a class, etc.) to tryand keep it from changing?
有没有一种方法可以在不定义常量的情况下声明常量(作为类的成员或不作为类的成员),并在确定后分配计算值一次(且仅一次);还是我将不得不诉诸非常量声明并使用编码 S&P(ALL_CAPS
变量名称、static
类中的声明等)来尝试防止它发生变化?
CLARIFICATION:
澄清:
Though these are good answers, the real-world situation I have is more complicated:
虽然这些都是很好的答案,但我的实际情况更为复杂:
The program has a main loop that continually runs between processing and rendering; the user can set required options and once they are set they will never change until the program is restart. An "Initialize" function is set up for anything that can be determined before the main loop, but values that are dependent on user interaction must be performed in the middle of the loop during the processing phase. (At the moment, persistent data storage techniques come to mind...)
该程序有一个主循环,在处理和渲染之间不断运行;用户可以设置所需的选项,一旦设置,它们将永远不会改变,直到程序重新启动。“初始化”函数是为任何可以在主循环之前确定的东西设置的,但是在处理阶段期间必须在循环中间执行依赖于用户交互的值。(此刻,我想到了持久数据存储技术......)
采纳答案by Oliver Charlesworth
Something like this?
像这样的东西?
const int x = calcConstant();
If it's a class member, then use the constructor initialisation list, as in Yuushi's answer.
如果它是类成员,则使用构造函数初始化列表,如 Yuushi 的回答。
回答by Yuushi
You can define it in a struct
or class
and utilize an initialisation list:
您可以在struct
or 中定义它class
并使用初始化列表:
#include <iostream>
struct has_const_member
{
const int x;
has_const_member(int x_)
: x(x_)
{ }
};
int main()
{
int foo = 0;
std::cin >> foo;
has_const_member h(foo);
std::cout << h.x << "\n";
return 0;
}
回答by Mankarse
As a static or function-local variable:
作为静态或函数局部变量:
const int x = calcConstant();
As a class member:
作为班级成员:
struct ConstContainer {
ConstContainer(int x) : x(x) {}
const int x;
};
回答by Mr Fooz
Yes, you can make a private static singleton field with an initialization method and a gettor method. Here's an example of how to do it:
是的,您可以使用初始化方法和 gettor 方法创建私有静态单例字段。以下是如何执行此操作的示例:
// In foo.h
class Foo
{
public:
// Caller must ensure that initializeGlobalValue
// was already called.
static int getGlobalValue() {
if (!initialized) {
... handle the error ...
}
return global_value;
}
static void initializeGlobalValue(...)
private:
static bool initialized;
static int global_value;
};
// In foo.cpp
bool Foo::initialized = false;
int Foo::global_value;
void Foo::initializeGlobalValue(...) {
if (initialized) {
...handle the error...
}
global_value = ...;
initialized = true;
}