dimanche 27 septembre 2015

Conditional expression behavior incorrect

I would expect that the two conditionals would yield the same answer (nEntries = 0). I don't understand why it doesn't.

# include <stdlib.h>
# include <iomanip>
# include <iostream>

using namespace std;

    int main() {
       int nEntries = 1;
       nEntries = (nEntries)? nEntries--: nEntries;
       cout << "nEntries = " << nEntries << endl;   // nEntries = 1
       nEntries = 1;
       if (nEntries) nEntries--;                    // nEntries = 0
       cout << "nEntries = " << nEntries << endl;
       return 0;
}

It looks like the conditional expression is doing:

if (nEntries) tmp = nEntries--;

art

Aucun commentaire:

Enregistrer un commentaire