初始化静态类变量 [英] initializing static class variables

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

问题描述

你好,


当一个std :: map< int,int> foob​​ar的;"声明出现在C ++程序中

后跟一个声明形式为foobar [123];的声明。没有任何东西

被插入到地图foobar的第123位,C ++标准规定

在123位创建一个新元素并初始化为

零。也就是说,在这种情况下,对(123,0)被插入

地图。


我有兴趣知道标准应该说什么发生在

以下案例中。在引用hello [110]时,标准是否要求struct Foo的指针是/ b $ b初始化为零,或者这个

程序导致未定义的行为。


感谢您的反馈和解释,


Neil


#include< iostream>

#include< map>


struct Foo {

int * x;

int * y;

};


typedef std :: map< int,Foo>你好;


int main(){

你好你好;

//初始化entiry结构,因此也是它的指针,零

//(我相信这也符合ANSI / ISO C ++标准)

std :: cout<<你好[110] .x<< std :: endl;

//因此解除引用零内存地址会导致段错误

// std :: cout<< *你好[110] .x<< std :: endl;

//因此同样用于分配

* hello [110] .x = 112;

}

解决方案



我相信STL标准说将使用默认构造函数构造一个对象
这个情况。由于你没有

一个,当你使用
插入一个新密钥时,x和y的值将是未定义的(垃圾)

dave


" Neil Zanella" < NZ ****** @ cs.mun.ca>在消息中写道

news:b6 ************************** @ posting.google.c om ...

你好,

当一个std :: map< int,int> foob​​ar的;"声明出现在C ++程序中,然后是foobar [123];形式的声明。没有任何东西已经被插入到地图foobar的123位,C ++标准规定
在123位创建一个新元素并初始化为零。也就是说,在这种情况下,对(123,0)被插入到
地图中。

我有兴趣了解标准所说的应该发生在
以下案件。在引用hello [110]时,标准是否要求struct Foo的指针初始化为零,或者这个
程序是否会导致未定义的行为。
感谢您的反馈和解释,

Neil

#include< iostream>
#include< map>

struct Foo {
int * x;
int * y;
};

typedef std :: map< int,Foo>您好;

int main(){
Hello hello;
//初始化entiry结构,因此它的指针也为零
//(我相信这是根据ANSI / ISO C ++标准也是如此)
std :: cout<<你好[110] .x<< std :: endl;
//因此解除引用零内存地址会导致段错误
// std :: cout<< *你好[110] .x<< std :: endl;
//因此同样适用于分配
* hello [110] .x = 112;
}



2004年7月29日星期四18:07:27 -0700,Dave Townsend< da ******** @ comcast.net>

写道:


我相信STL标准说在这种情况下将使用默认构造函数构建一个对象。由于你没有
,当你插入一把新钥匙时,x和y的值将是未定义的(垃圾)

dave


我不相信这是正确的,标准说你好[110]是

等于


(*( (insert(make_pair(100,Foo())))。first))。second;


关键位是Foo(),这是值初始化而不是默认值

初始化。对于Foo的值初始化,x和y将为零

初始化。


但是我认为值iniitalisation是一个相当新的概念

C ++标准,如果编译器出错,我不会感到惊讶。


我也不完全确定这一点,我真的只是张贴到

放置替代视图。标准的相关部分是8.5,

23.3.1.2,5.2.3对于任何想要查看它的人。也许其中一个

大师可以清除它。


无论哪种方式,安全的做法是向Foo添加一个默认构造函数。


john

" Neil Zanella" < NZ ****** @ cs.mun.ca>在消息中写道
新闻:b6 ************************** @ posting.google.c om ...

你好,

当一个std :: map< int,int> foob​​ar的;"声明出现在C ++程序中,然后是foobar [123];形式的声明。在没有任何东西被插入到地图foobar的位置123处,C ++标准声明
在123位创建一个新元素并初始化为零。也就是说,在这种情况下,对(123,0)被插入到
地图中。

我有兴趣了解标准所说的应该发生在
以下案件。在引用hello [110]时,标准是否要求struct Foo的指针初始化为零,或者这个
程序是否会导致未定义的行为。
感谢您的反馈和解释,

Neil

#include< iostream>
#include< map>

struct Foo {
int * x;
int * y;
};

typedef std :: map< int,Foo>您好;

int main(){
Hello hello;
//初始化entiry结构,因此它的指针也为零
//(我相信这是根据ANSI / ISO C ++标准也是如此)
std :: cout<<你好[110] .x<< std :: endl;
//因此解除引用零内存地址会导致段错误
// std :: cout<< *你好[110] .x<< std :: endl;
//因此同样适用于分配
* hello [110] .x = 112;
}





John,


感谢您指出这一微妙之处。你能发一个例子吗?

演示了它的不同之处,我听说过提到这两种类型的初始化但是从未接受过这种差异。


谢谢,戴夫


" John Harrison" <乔************* @ hotmail.com>在消息中写道

news:opsbxp9wq7212331 @ andronicus ...

2004年7月29日星期四18:07:27 -0700,Dave Townsend< da **** ****@comcast.net>
写道:


我相信STL标准说将使用默认构造函数构建一个对象
在这种情况下。由于你没有
一个,当你插入一个新的密钥时,x和y的值将是未定义的(垃圾)

dave



我不相信这是正确的,标准说你好[110]等同于

(*((insert(make_pair(100,Foo) ())))。第一))。第二;

关键位是Foo(),这是值初始化而不是默认初始化。对于Foo的值初始化,x和y将初始化为零。

然而,我认为在C ++标准中,值iniitalisation是一个相当新的概念,我不会如果编译器弄错了,我会感到惊讶。

我也不完全确定这一点,而且我真的只是发帖给
提出另类视图。标准的相关部分是8.5,
23.3.1.2,5.2.3对于任何想要查看它的人。也许其中一位大师可以清除它。

无论哪种方式,安全的做法是向Foo添加一个默认构造函数。

john

" Neil Zanella" < NZ ****** @ cs.mun.ca>在消息中写道
新闻:b6 ************************** @ posting.google.c om ...

你好,

当一个std :: map< int,int> foob​​ar的;"声明出现在C ++程序中,然后是foobar [123];形式的声明。在没有任何东西被插入到地图foobar的位置123处,C ++标准声明
在123位创建一个新元素并初始化为零。也就是说,在这种情况下,对(123,0)被插入到
地图中。

我有兴趣了解标准所说的应该发生在
以下案件。在引用hello [110]时,标准是否要求struct Foo的指针初始化为零,或者这个
程序是否会导致未定义的行为。
感谢您的反馈和解释,

Neil

#include< iostream>
#include< map>

struct Foo {
int * x;
int * y;
};

typedef std :: map< int,Foo>您好;

int main(){
Hello hello;
//初始化entiry结构,因此它的指针也为零
//(我相信这是根据ANSI / ISO C ++标准也是如此)
std :: cout<<你好[110] .x<< std :: endl;
//因此解除引用零内存地址会导致段错误
// std :: cout<< *你好[110] .x<< std :: endl;
//因此同样适用于分配
* hello [110] .x = 112;
}




Hello,

When an "std::map<int, int> foobar;" statement appears in a C++ program
followed by a statement of the form "foobar[123];" where nothing has ever
been inserted at position 123 into map foobar, the C++ standard states
that a new element is created at position 123 and is initialized to
zero. That is, in this case, the pair (123, 0) is inserted into the
map.

I am interested in knowing what the standard says should happen in the
following case. Does the standard mandate that struct Foo''s pointers be
initialized to zero as well, upon reference to hello[110], or does this
program lead to undefined behavior.

Thank you for your feedback and explanations,

Neil

#include <iostream>
#include <map>

struct Foo {
int *x;
int *y;
};

typedef std::map<int, Foo> Hello;

int main() {
Hello hello;
//initialize entiry structure, hence also its pointers, to zero
//(I believe this is according to the ANSI/ISO C++ standard too)
std::cout << hello[110].x << std::endl;
//hence dereferencing zero memory address causes segfault
//std::cout << *hello[110].x << std::endl;
//hence same goes for assigning
*hello[110].x = 112;
}

解决方案


I believe the STL standard says an object will be constructed
with the default constructor in this situation. Since you don''t have
one, the values of x and y will be undefined (garbage) when you
insert a new key

dave

"Neil Zanella" <nz******@cs.mun.ca> wrote in message
news:b6**************************@posting.google.c om...

Hello,

When an "std::map<int, int> foobar;" statement appears in a C++ program
followed by a statement of the form "foobar[123];" where nothing has ever
been inserted at position 123 into map foobar, the C++ standard states
that a new element is created at position 123 and is initialized to
zero. That is, in this case, the pair (123, 0) is inserted into the
map.

I am interested in knowing what the standard says should happen in the
following case. Does the standard mandate that struct Foo''s pointers be
initialized to zero as well, upon reference to hello[110], or does this
program lead to undefined behavior.

Thank you for your feedback and explanations,

Neil

#include <iostream>
#include <map>

struct Foo {
int *x;
int *y;
};

typedef std::map<int, Foo> Hello;

int main() {
Hello hello;
//initialize entiry structure, hence also its pointers, to zero
//(I believe this is according to the ANSI/ISO C++ standard too)
std::cout << hello[110].x << std::endl;
//hence dereferencing zero memory address causes segfault
//std::cout << *hello[110].x << std::endl;
//hence same goes for assigning
*hello[110].x = 112;
}



On Thu, 29 Jul 2004 18:07:27 -0700, Dave Townsend <da********@comcast.net>
wrote:


I believe the STL standard says an object will be constructed
with the default constructor in this situation. Since you don''t have
one, the values of x and y will be undefined (garbage) when you
insert a new key

dave

I don''t believe that is correct, the standard says that hello[110] is
equivalvent to

(*((insert(make_pair(100,Foo()))).first)).second;

he crucial bit is Foo() which is value initialisation not default
initialisation. For value initialisation of Foo, x and y will be zero
initialised.

However I think value iniitalisation is a fairly new concept in the
C++ standard and I wouldn''t be surprised if a compiler got this wrong.

Also I''m not completely certain of this, and I''m really just posting to
put the alternative view. The releveant sections of the standard are 8.5,
23.3.1.2, 5.2.3 for anyone who want''s to check this out. Maybe one of the
gurus could clear this up.

Either way the safe thing to do is to add a default constructor to Foo.

john
"Neil Zanella" <nz******@cs.mun.ca> wrote in message
news:b6**************************@posting.google.c om...

Hello,

When an "std::map<int, int> foobar;" statement appears in a C++ program
followed by a statement of the form "foobar[123];" where nothing has
ever
been inserted at position 123 into map foobar, the C++ standard states
that a new element is created at position 123 and is initialized to
zero. That is, in this case, the pair (123, 0) is inserted into the
map.

I am interested in knowing what the standard says should happen in the
following case. Does the standard mandate that struct Foo''s pointers be
initialized to zero as well, upon reference to hello[110], or does this
program lead to undefined behavior.

Thank you for your feedback and explanations,

Neil

#include <iostream>
#include <map>

struct Foo {
int *x;
int *y;
};

typedef std::map<int, Foo> Hello;

int main() {
Hello hello;
//initialize entiry structure, hence also its pointers, to zero
//(I believe this is according to the ANSI/ISO C++ standard too)
std::cout << hello[110].x << std::endl;
//hence dereferencing zero memory address causes segfault
//std::cout << *hello[110].x << std::endl;
//hence same goes for assigning
*hello[110].x = 112;
}





John,

thanks for point out this subtlety. Can you post an example which
demonstrates the difference, I''ve heard mention of these two types
of initialization but never got on to what the difference was.

thanks, dave

"John Harrison" <jo*************@hotmail.com> wrote in message
news:opsbxp9wq7212331@andronicus...

On Thu, 29 Jul 2004 18:07:27 -0700, Dave Townsend <da********@comcast.net>
wrote:


I believe the STL standard says an object will be constructed
with the default constructor in this situation. Since you don''t have
one, the values of x and y will be undefined (garbage) when you
insert a new key

dave



I don''t believe that is correct, the standard says that hello[110] is
equivalvent to

(*((insert(make_pair(100,Foo()))).first)).second;

he crucial bit is Foo() which is value initialisation not default
initialisation. For value initialisation of Foo, x and y will be zero
initialised.

However I think value iniitalisation is a fairly new concept in the
C++ standard and I wouldn''t be surprised if a compiler got this wrong.

Also I''m not completely certain of this, and I''m really just posting to
put the alternative view. The releveant sections of the standard are 8.5,
23.3.1.2, 5.2.3 for anyone who want''s to check this out. Maybe one of the
gurus could clear this up.

Either way the safe thing to do is to add a default constructor to Foo.

john

"Neil Zanella" <nz******@cs.mun.ca> wrote in message
news:b6**************************@posting.google.c om...

Hello,

When an "std::map<int, int> foobar;" statement appears in a C++ program
followed by a statement of the form "foobar[123];" where nothing has
ever
been inserted at position 123 into map foobar, the C++ standard states
that a new element is created at position 123 and is initialized to
zero. That is, in this case, the pair (123, 0) is inserted into the
map.

I am interested in knowing what the standard says should happen in the
following case. Does the standard mandate that struct Foo''s pointers be
initialized to zero as well, upon reference to hello[110], or does this
program lead to undefined behavior.

Thank you for your feedback and explanations,

Neil

#include <iostream>
#include <map>

struct Foo {
int *x;
int *y;
};

typedef std::map<int, Foo> Hello;

int main() {
Hello hello;
//initialize entiry structure, hence also its pointers, to zero
//(I believe this is according to the ANSI/ISO C++ standard too)
std::cout << hello[110].x << std::endl;
//hence dereferencing zero memory address causes segfault
//std::cout << *hello[110].x << std::endl;
//hence same goes for assigning
*hello[110].x = 112;
}




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

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