Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ arrow operator equivelence

Tags:

c++

pointers

The posts What can I use instead of the arrow operator, `->`? and Arrow operator (->) usage in C state

The following two expressions are equivalent:

x->y
(*x).y

But this does not appear to always be true when taken as a mathematical equivalence.

Why does g++ throw an error when replacing

a->b->c

with

a->(*b).c

?

It seems the above equivalence is not always replaceable. Therefore, I think the term "equivalent" is a bit misleading.

Also, I am not referring to any sort of overloading in this question.

like image 596
Tommy Avatar asked Nov 28 '22 17:11

Tommy


1 Answers

You've got the associativity rules wrong. a->b->c is (a->b)->c, not a->(b->c), so it becomes (*(a->b)).c (and then (*((*a).b)).c).

like image 57
R. Martinho Fernandes Avatar answered Dec 11 '22 04:12

R. Martinho Fernandes