C++ arrow operator with pre-increment: With or without parentheses is the same?

Ely

Question from the course:

Watch the parentheses around the argument of the ++ operator. Are they really needed? What will happen when you remove them?

Initially there was only one cout expression. I added another one to see the difference, like so:

#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;
    ...

The result is the same in both cases. Thus I conclude: No difference.

Can someone explain and correct please? Why would they ask that question if there is no difference? My feeling is there is a subtlety I am missing.

Result:

Object constructed!
1
2
Object destructed!
TartanLlama

There is no difference because -> has a higher precedence than ++. This means that ++ptr -> value is always parsed as ++(ptr->value).

Regardless of how the compiler will see your code, you shouldn't write it like that, because someone who doesn't know C++ operator precedence rules might think the code does something different from what it actually does. ++(ptr->value) is far clearer.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Unexpected behaviour of pre increment operator in C

From Dev

Precendence of the arrow operator and post increment

From Dev

Precedence of the arrow operator and post increment?

From Dev

pre/post increment operator on OutputIterator

From Dev

pre increment operator that returns void

From Dev

Pre/post increment/decrement and operator order confusion

From Dev

Pre/post increment/decrement and operator order confusion

From Dev

Pointer to pointer dereference with pre-increment operator

From Dev

Array increment operator in C

From Dev

Understanding the increment operator in C

From Dev

Pre-increment operation in C

From Dev

C++ increment operator and sum

From Dev

C++ increment ++ operator overloading

From Dev

C++ overloading increment operator

From Dev

Why does the pre/post increment operator behave wrongly?

From Dev

Does the placement of a pre-increment operator make a difference here?

From Dev

overloading pre-increment operator not showing correct result

From Dev

Input in array without adding increment operator ++

From Dev

C - pointer behavior with pre and post increment

From Dev

Pre/Post Increment Pointers in C++

From Dev

Java - Having AND and OR in the same condition, without parentheses

From Dev

Pre-Increment Operators when Using the Variable on the Same Line

From Dev

How to increment the value in Hash/Object without pre-defining them

From Java

Why does the arrow (->) operator in C exist?

From Dev

Pre increment operator and dereference operator resulting in segmentation fault, can't seem to understand why

From Dev

C operator precendence: Increment and logical operators

From Dev

Prefix increment operator error C++

From Dev

C operator precendence: Increment and logical operators

From Dev

Not getting correct results with the increment operator in C++

Related Related

  1. 1

    Unexpected behaviour of pre increment operator in C

  2. 2

    Precendence of the arrow operator and post increment

  3. 3

    Precedence of the arrow operator and post increment?

  4. 4

    pre/post increment operator on OutputIterator

  5. 5

    pre increment operator that returns void

  6. 6

    Pre/post increment/decrement and operator order confusion

  7. 7

    Pre/post increment/decrement and operator order confusion

  8. 8

    Pointer to pointer dereference with pre-increment operator

  9. 9

    Array increment operator in C

  10. 10

    Understanding the increment operator in C

  11. 11

    Pre-increment operation in C

  12. 12

    C++ increment operator and sum

  13. 13

    C++ increment ++ operator overloading

  14. 14

    C++ overloading increment operator

  15. 15

    Why does the pre/post increment operator behave wrongly?

  16. 16

    Does the placement of a pre-increment operator make a difference here?

  17. 17

    overloading pre-increment operator not showing correct result

  18. 18

    Input in array without adding increment operator ++

  19. 19

    C - pointer behavior with pre and post increment

  20. 20

    Pre/Post Increment Pointers in C++

  21. 21

    Java - Having AND and OR in the same condition, without parentheses

  22. 22

    Pre-Increment Operators when Using the Variable on the Same Line

  23. 23

    How to increment the value in Hash/Object without pre-defining them

  24. 24

    Why does the arrow (->) operator in C exist?

  25. 25

    Pre increment operator and dereference operator resulting in segmentation fault, can't seem to understand why

  26. 26

    C operator precendence: Increment and logical operators

  27. 27

    Prefix increment operator error C++

  28. 28

    C operator precendence: Increment and logical operators

  29. 29

    Not getting correct results with the increment operator in C++

HotTag

Archive