C++ 修改C++中的静态成员变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13442909/
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
modify a static member variable in C++
提问by yang
I am trying to define a class Util with a static member variable MAX_DIST, in the following sense,
我试图用静态成员变量 MAX_DIST 定义一个类 Util,在以下意义上,
class Util{
public:
static double MAX_DIST;
Util():MAX_DIST(400.0){}
};
and be able to update it in some other class, e.g.
并能够在其他类中更新它,例如
Util::MAX_DIST = 387.98;
This gives me an error:
这给了我一个错误:
‘double Util::MAX_DIST' is a static data member; it can only be initialized at its definition
However, if I initialize MAX_DIST at its definition, such as
但是,如果我在其定义处初始化 MAX_DIST,例如
class Util{
public:
static const double MAX_DIST = 400;
Util();
};
(I have to add the 'const' as instructed by the compiler, otherwise I will get an "ISO C++ forbids in-class initialization of non-const static member" error) Now I can not modify MAX_DIST in other places since it is now ready only:
(我必须按照编译器的指示添加'const',否则我会得到一个“ISO C++禁止非常量静态成员的类内初始化”错误)现在我不能在其他地方修改MAX_DIST,因为它是现在只准备好:
error: assignment of read-only variable ‘Util::MAX_DIST'
After fruitless search on the internet, I can not find a solution to this paradox. Can someone help me out?
在互联网上搜索无果后,我找不到解决这个悖论的方法。有人可以帮我吗?
采纳答案by Konrad Rudolph
However, if I initialize MAX_DIST at its definition
但是,如果我在其定义处初始化 MAX_DIST
You're confusing definition and declaration. You are trying to initialise in the latter – C++ forbids that. The other answers have shown you how the definitionlooks like: it has to be outsidethe class declaration, and in its own compilation unit (otherwise you'll violate the One Definition Rule when you try including the header in several source files).
你混淆了定义和 声明。你试图在后者中初始化——C++ 禁止这样做。其他答案向您展示了定义的样子:它必须在类声明之外,并在其自己的编译单元中(否则,当您尝试在多个源文件中包含头文件时,您将违反单一定义规则)。
A word on why the original code didn't work: you tried putting the initialisation into the initialiser list of the constructor. However, this constructor is called for every instance. Even if that code compiled, it would always reset your static variable – not what you want.
关于为什么原始代码不起作用的一句话:您尝试将初始化放入构造函数的初始化列表中。但是,每个实例都会调用此构造函数。即使编译了该代码,它也总是会重置您的静态变量——而不是您想要的。
回答by Vincenzo Pii
Put this in your Util.cpp
(or whatever the filename is) file:
把它放在你的Util.cpp
(或任何文件名)文件中:
double Util::MAX_DIST = 0;
Static variables need to be initialized.
静态变量需要初始化。
Long answer, quoting the standard 9.4.2 $2
:
长答案,引用标准9.4.2 $2
:
The declaration of a static data member in its class definition is not a definition and may be of an incomplete type other than cv-qualified void. The definition for a static data member shall appear in a namespace scope enclosing the member's class definition. In the definition at namespace scope, the name of the static data member shall be qualified by its class name using the :: operator. The initializer expression in the definition of a static data member is in the scope of its class (3.3.7).
静态数据成员在其类定义中的声明不是定义,并且可能是除 cv 限定的 void 之外的不完整类型。静态数据成员的定义应出现在包含成员类定义的命名空间范围内。在命名空间范围的定义中,静态数据成员的名称应使用 :: 运算符由其类名限定。静态数据成员定义中的初始化表达式在其类 (3.3.7) 的范围内。
回答by Xymostech
In the first case, you are trying to initialize a static variable from within a non-static context, i.e. from within a constructor. You are correct that this is wrong.
在第一种情况下,您试图从非静态上下文中初始化静态变量,即从构造函数中。你是对的,这是错误的。
In the second case, you do not want your variable to be const. Instead, you need to declare it outside of the class, using a statement like:
在第二种情况下,您不希望变量为 const。相反,您需要在类之外声明它,使用如下语句:
double Util::MAX_DIST = 400;