使用cout(C ++)打印未初始化的bool [英] Printing an uninitialized bool using cout (C++)

查看:146
本文介绍了使用cout(C ++)打印未初始化的bool的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类与 bool 数据成员,没有被构造函数初始化。如果我这样做

I have a class with a bool data member that is not initialized by the constructor. If I do

cout << x.myBoolDataMember;

其中 x 其中 bool 没有被初始化,我有时得到一个随机数而不是0或1.(我使用 gcc 。)此行为是否符合标准

where x is an object of this class in which the bool has not been initialized, I sometimes get a random number rather than 0 or 1. (I'm using gcc.) Is this behavior compliant with the Standard?

推荐答案


这种行为是否符合标准?

Is this behavior compliant with the standard?

是的!在代码中使用垃圾值(未初始化)调用未定义行为

Yes! Using garbage values(uninitialized) in your code invokes Undefined Behavior

这篇关于使用cout(C ++)打印未初始化的bool的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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