关于初始化的问题...... [英] A Question about initialization...

查看:92
本文介绍了关于初始化的问题......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有静态成员变量的类。


string x;


x在使用过程中永远不会改变,应该初始化到abcd。


如何做到这一点?

解决方案

JustSomeGuy写道:

我有一个有静态成员变量的类。

字符串x;

x在使用过程中永远不会改变,应该初始化为abcd ;

如何做到这一点?




const string x =" abcd";


- Pete


JustSomeGuy写道:

我有一个有静态成员变量的类。

字符串x;

x在使用过程中永远不会改变,应该初始化为abcd。

如何做到这一点?



例如:

class X {

public:


static

const std :: string str; //首先申报。


}; // X


const std :: string X :: str(" abcd"); //稍后初始化。

请注意,令我非常沮丧的是,


class X {

public:


static

const std :: string str(" abcd"); // 一气呵成?不!


}; // X


确实*不*工作。实际上,我不知道原因。
最好


Kai-Uwe


"皮特C. < x@x.x>写道:

JustSomeGuy写道:

我有一个有静态成员变量的类。

字符串x;
<在使用过程中x应该永远不会改变,并且应该初始化为abcd。

如何做到这一点?



const string x =" ; abcd" ;;




你不能在课堂上这样做。你是正确的,成员有
是const,但初始化应该在构造函数中发生。


V


I have a class that has a static member variable.

string x;

x should never change during use and should be intialized to "abcd".

How does one do this?

解决方案

JustSomeGuy wrote:

I have a class that has a static member variable.

string x;

x should never change during use and should be intialized to "abcd".

How does one do this?



const string x = "abcd";

- Pete


JustSomeGuy wrote:

I have a class that has a static member variable.

string x;

x should never change during use and should be intialized to "abcd".

How does one do this?


For instance:
class X {
public:

static
const std::string str; // declare first.

}; // X

const std::string X::str ( "abcd" ); // initialize later.
Note that, to my great dismay,

class X {
public:

static
const std::string str ( "abcd" ); // in one go? no!

}; // X

does *not* work. Actually, I do not know the reason.
Best

Kai-Uwe


"Pete C." <x@x.x> wrote...

JustSomeGuy wrote:

I have a class that has a static member variable.

string x;

x should never change during use and should be intialized to "abcd".

How does one do this?



const string x = "abcd";



You cannot do this in a class. You''re correct that the member has
to be const, but the initialisation should happen in a constructor.

V


这篇关于关于初始化的问题......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆