如何将静态常量变量保留为类的成员

发布于 2024-09-18 16:21:33 字数 503 浏览 4 评论 0原文

我想保留一个静态常量变量作为类的成员。 是否可以保留该变量以及如何初始化该变量。

有些人通过说这个

 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

花伊自在美 2024-09-25 16:21:33

在源文件中的任何函数之外 write:

const QString ClassName::ALARM_WARNING_IMAGE = "warning.png";

此构造也有效:

const QString ClassName::ALARM_WARNING_IMAGE("warning.png");

标头:

class ClassName {
  static const QString ALARM_WARNING_IMAGE;
};

另外,不要在构造函数中写入任何内容。每次实例化 ClassName 时都会初始化静态变量。这是行不通的,因为变量是 const,可以说是一个坏主意。 const 在声明期间只能设置一次。

Outside of any function in the source file write:

const QString ClassName::ALARM_WARNING_IMAGE = "warning.png";

This construction also works:

const QString ClassName::ALARM_WARNING_IMAGE("warning.png");

Header:

class ClassName {
  static const QString ALARM_WARNING_IMAGE;
};

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.

土豪 2024-09-25 16:21:33

这是基本思想:

struct myclass{
 //myclass() : x(2){}      // Not OK for both x and d
 //myclass(){x = 2;}       // Not OK for both x and d
 static const int x = 2;   // OK, but definition still required in namespace scope
                               // static integral data members only can be initialized
                               // in class definition
     static const double d;    // declaration, needs definition in namespace scope,
                               // as double is not an integral type, and so is
                               // QSTRING.
     //static const QString var; // non integral type
};

const int myclass::x;             // definition
const double myclass::d = 2.2;    // OK, definition
// const QString myclass::var = "some.png";

int main(){
}

Here is the basic idea:

struct myclass{
 //myclass() : x(2){}      // Not OK for both x and d
 //myclass(){x = 2;}       // Not OK for both x and d
 static const int x = 2;   // OK, but definition still required in namespace scope
                               // static integral data members only can be initialized
                               // in class definition
     static const double d;    // declaration, needs definition in namespace scope,
                               // as double is not an integral type, and so is
                               // QSTRING.
     //static const QString var; // non integral type
};

const int myclass::x;             // definition
const double myclass::d = 2.2;    // OK, definition
// const QString myclass::var = "some.png";

int main(){
}
游魂 2024-09-25 16:21:33

尝试:

QString ClassName::ALARM_WARNING_IMAGE = "warning.png";

Try:

QString ClassName::ALARM_WARNING_IMAGE = "warning.png";

婴鹅 2024-09-25 16:21:33

仅允许在类或结构内部初始化 const static 整型数据成员。

Only const static integral data members are allowed to initialized inside a class or struct.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文