一个“if”条件求值为true会导致下一个if语句计算为true

时间:2014-11-28 06:00:57

标签: c++ if-statement cout

当下面代码的第一个if语句求值为true时,奇怪的是第二个if语句的计算结果为true。更奇怪的是,如果我在第一个语句中注释掉cout行,则第二个if语句不会评估为true(应该是这样)。

cout没有被注释掉并且第一个条件的计算结果为真时,我会得到cities[202].getNext()->getX()的一些非常奇怪的值,它会在第二个if语句代码块中打印出来。

顺便说一句,这段代码只是执行一些单元测试,因此是硬编码值。在第一个if语句中,cities[202].getID()应该== 202,以便if语句的计算结果为false。但是,我正在测试我的测试代码,因此我将cities[202].getID() != 202更改为cities[202].getID() != 201

// test middle element of vect
    if( cities[202].getID() != 201 /*should be 202, wrote 201 on purpose*/ || cities[202].getX() != 188 || cities[202].getY() != 41){
        pass = 0;
         // second conditional below fails (which is good) if I do not comment out this next line with cout
        cout << "error with middle city x, y or id" << endl;
    }
        // middle element's next and prev should point to Default obj, with id=777, x=77, y=7 
    if( cities[202].getNext()->getX() != 77 || cities[202].getNext()->getID() != 777){
        pass = 0;
        cout << "error with middle city next" << endl;
        cout << "cities[202].getNext->getX = " << cities[202].getNext()->getX() << endl;
    }

出现这种情况的原因有何想法? cout怎么可能导致这样的事情?

0 个答案:

没有答案
相关问题