// Several things that are fully specified in Java are NOT specified in C++. // // For example: // // - values of variables // The values of these variables are not specified by C++. Any values // are possible. The values are often zero, because some operating // systems clear out memory before giving it to a program. It would be // poor security if your program could read the values left over in // memory from the previous program. // // - order of operand evaluation // foo() + bar(): which gets called first? Could be either. // // - pre-increment/post-increment evaluation order // a++ * a++ is just evil // // The solution to the last two problems is to use explicit // temporary variables: temp=foo(); temp+=bar(); // // Compilers are NOT required to warn about these problems, though g++ // warns about SOME problems when using -Wall. // // It astonishes me that there are people with enough brainpower to chew // their food, but still stupid enough to avoid -Wall. // // I use: g++ -Wall -Wextra -Wpedantic #include using namespace std; int foo() { cout << "foo got called\n"; return 1; } int bar() { cout << "bar got called\n"; return 2; } int main() { int a = foo() + bar(); // Which gets called first, foo or bar? int b = ++a + ++a; // When do the increments happen? b = b++; // What’s first, assignment or postincrement? double d, e; cout << "a=" << a << " b=" << b << " d=" << d << " e=" << e << '\n'; return 0; }