c++ - 三重平等条件带来奇怪的结果

标签 c++ debugging visual-studio-2012

<分区>

我正在编写一个简单的代码来按后序迭代打印二叉树,但我偶然发现了这个奇怪的调试步骤:

enter image description here

图像未经过任何方式的 Photoshop 或修改,“locals”窗口显示两个指针具有不同的值,但是当将它们与自身和 NULL 进行比较时,相等返回 true 和“Wat??”打印出来了,怎么会??

我错过了什么吗?

如果我将其重写为

if(pointer1 == NULL && pointer2 == NULL)
    cout << "This won't be printed";

正常运行

最佳答案

相等比较运算符 (operator ==) 是一个二元运算符,它与左边相关联。因此,您的情况:

pointer1 == pointer2 == NULL

变成:

(pointer1 == pointer2) == NULL,变成:

true == NULL 如果 (pointer1 == pointer2);或

false == NULL 如果 (pointer1 != pointer2)

由于 NULL 转换为 bool 值 false,当且仅当 pointer1 != pointer2 时,此条件计算为 true >.

如您所见,这与:

(pointer1 == NULL && pointer2 == NULL)

当且仅当 pointer1pointer2 都不为 NULL 时,它的计算结果为 true

关于c++ - 三重平等条件带来奇怪的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15223813/

相关文章:

c++ - 在 C++ 中居中 Pascal 的三角形输出

ios - iOS游戏开始时的输入延迟

c++ - 在 VS2012 中使用 sqrt 内在的堆栈运行时检查失败

c# - "Publish language"选项到底是什么?

c++ - Qt : which class should connect() signals and slots - inside or outside the view class?

c++ - 在 C++ 中生成和存储随机数组

C++ sqlite3 绑定(bind)参数

c# - 如何在 WPF 中调试绑定(bind)

c# - 调试时忽略特定文件

c++ - 项目仅在特定版本的 Visual Studio 中编译