const对象声明 [英] const object declaration

查看:95
本文介绍了const对象声明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下程序:


#include< iostream>


使用命名空间std;


class X

{

public:

int value()const {return val; }

int val;

};


int main()

{

const X obj;


返回0;

}


编译此程序时用g ++作为

g ++ -std = c ++ 98 -pedantic -Wall -Wextra x.cpp


为行const生成以下编译错误X obj;


错误:未初始化const`obj''


但VC ++ 2005 Express Edition仅提供以下警告(无

错误):


警告:''obj'':''const''用编译器初始化的自动数据

生成的默认构造函数产生不可靠的结果


根据标准,这是正确的行为吗?


请澄清。


另请告诉我如何设置VC ++ 2005中的编译标志

Express Edition IDE类似于命令行g ++编译标志

如上所述。

谢谢

V.Subramanian

Consider the following program:

#include <iostream>

using namespace std;

class X
{
public:
int value() const { return val; }
int val;
};

int main()
{
const X obj;

return 0;
}

When this program is compiled with g++ as
g++ -std=c++98 -pedantic -Wall -Wextra x.cpp

the following compilation error is generated for the line const X obj;

error: uninitialized const `obj''

However VC++ 2005 Express Edition gives only the following warning(no
error):

warning: ''obj'' : ''const'' automatic data initialized with compiler
generated default constructor produces unreliable results

Which is the correct behaviour as per the standard ?

Kindly clarify.

Also please let me know how to set the compilation flags in VC++ 2005
Express Edition IDE similar to the command line g++ compilation flags
as mentioned above.

Thanks
V.Subramanian

推荐答案

根据标准,这是正确的行为?


我不知道。但我的赌注是,如果你定义一个空构造函数,你的

问题应该消失。
Which is the correct behaviour as per the standard ?

I don''t know. But my bet is if you define an empty constructor your
problem should go away.

>

请澄清。


另外请告诉我如何在VC ++ 2005中设置编译标志

Express Edition IDE类似于命令行g ++编译标志
>
Kindly clarify.

Also please let me know how to set the compilation flags in VC++ 2005
Express Edition IDE similar to the command line g++ compilation flags
as mentioned above.



毫无疑问,gcc小组会更好地询问。


Ben

That, im afraid, is better asked in the gcc group.

Ben


benben写道:
benben wrote:

> [..]
另外请告诉我如上所述,在VC ++ 2005
Express Edition IDE中设置类似于命令行g ++编译标志的编译标志。
>[..]
Also please let me know how to set the compilation flags in VC++ 2005
Express Edition IDE similar to the command line g++ compilation flags
as mentioned above.



我担心,gcc小组会更好。


That, im afraid, is better asked in the gcc group.



或者在VC ++新闻组中。


V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问

Or in the VC++ newsgroup.

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


12月3日,下午7:54,Victor Bazarov < v.Abaza ... @ comAcast.netwrote:
On Dec 3, 7:54 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:

benben写道:
benben wrote:

[..]

另外请让我知道如何在VC ++ 2005中设置编译标志

Express Edition IDE类似于命令行g ++编译旗帜

如上所述。
[..]
Also please let me know how to set the compilation flags in VC++ 2005
Express Edition IDE similar to the command line g++ compilation flags
as mentioned above.


我很害怕,gcc小组会更好。
That, im afraid, is better asked in the gcc group.



或者在VC ++新闻组中。


V


Or in the VC++ newsgroup.

V



我曾经在clc ++ m上问了一次,并没有得到令人信服的答案。用户定义的构造函数必须为b
提供的标准强制要求

。即使班上没有成员,也需要它。

它就是这样。我不知道这个

背后的理由。


如果班级有POD类型,它将保持未初始化

(不确定值)时的默认构造函数(由编译器生成
生成的默认构造函数)。如果是非POD成员,他们会默认初始化
。一个不确定的POD类型的值是

相当无用但是对于非POD类型,它可能是有意义的。我不知道
知道基本原理,但可能是基于这些线路上的某些东西,以避免复杂性。我不确定。可能是一些

大师会启发我们! :)

I asked this once on clc++m and had not got a convincing answer. It is
mandated by the standard that a user-defined constructor must be
provided. It is needed even though there are no members in the class.
It just is as it is. I am not aware of the rationale behind this
though.

In case the class has a POD type, it would remain uninitialized
(indeterminate value) when default constructor (by the compiler
generated default constructor). In case of non-POD members, they would
have been default initialized. An indeterminate POD type''s value is
rather useless but for non-POD types, it might make sense. I don''t
know about the rationale but may be it is based on something along
these lines, to avoid complexity. I don''t know for sure. May be some
guru will enlighten us! :)


这篇关于const对象声明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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