如何将静态常量变量保留为类的成员
我想保留一个静态常量变量作为类的成员。 是否可以保留该变量以及如何初始化该变量。
有些人通过说这个
QString <ClassName>::ALARM_ERROR_IMAGE = "error.png";
Initilizing value for a const data
我尝试过这样的
帮助在 CPP 类中我写
static QString ALARM_WARNING_IMAGE ;
在构造函数中我写
ALARM_WARNING_IMAGE = "warning.png";
但不工作...请通过给出一些提示来帮助
I want to keep a static const variable as a member of class.
Is it possible to keep and how can i initilize that variable.
Some body helped by saying this
QString <ClassName>::ALARM_ERROR_IMAGE = "error.png";
Initilizing value for a const data
I tried like this
in CPP class i write
static QString ALARM_WARNING_IMAGE ;
In constructor i write
ALARM_WARNING_IMAGE = "warning.png";
But not working... Please help by giving some hints
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在源文件中的任何函数之外 write:
此构造也有效:
标头:
另外,不要在构造函数中写入任何内容。每次实例化 ClassName 时都会初始化静态变量。这是行不通的,因为变量是 const,可以说是一个坏主意。 const 在声明期间只能设置一次。
Outside of any function in the source file write:
This construction also works:
Header:
Also, don't write anything in the constructor. This would initialize the static variable everytime ClassName is instantiated. This does not work, because the variable is const and a bad idea so to speak. consts can only be set once during declaration.
这是基本思想:
Here is the basic idea:
尝试:
QString ClassName::ALARM_WARNING_IMAGE = "warning.png";
Try:
QString ClassName::ALARM_WARNING_IMAGE = "warning.png";
仅允许在类或结构内部初始化 const static 整型数据成员。
Only const static integral data members are allowed to initialized inside a class or struct.