Question from the course:
Watch the parentheses around the argument of the ++ operator. Are they really necessary? What happens when you delete them?
Initially, there was only one expression cout. I added another to see the difference, for example:
#include <iostream>
using namespace std;
class Class {
public:
Class(void) {
cout << "Object constructed!" << endl;
}
~Class(void) {
cout << "Object destructed!" << endl;
}
int value;
};
int main(void) {
Class *ptr;
ptr = new Class;
ptr -> value = 0;
cout << ++(ptr -> value) << endl;
cout << ++(ptr -> value) << endl;
delete ptr;
return 0;
}
My idea was to test it again without parentheses and see what is different:
...
cout << ++ptr -> value << endl;
cout << ++ptr -> value << endl;
...
In both cases, the result is the same. So I conclude: no difference.
Can someone explain and correct, please? Why do they ask this question if there is no difference? I feel that there is a subtlety that I am lacking.
Result:
Object constructed!
1
2
Object destructed!
source
share