Operator with C ++ arrow with pre-increment: with or without parentheses the same?

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!
+4
source share
3 answers

, -> , ++. , ++ptr -> value ++(ptr->value).

, , , , ++, , - , . ++(ptr->value) .

+6

, -> , ++ () <<

, , . , , - .

, , postfix- ++. precende, ->, . , , ;)

+4

This is because selecting an element through a pointer (arrow) has a higher priority than the increment and decrement prefix operator. For more information see

http://en.cppreference.com/w/cpp/language/operator_precedence

+2
source

All Articles