The Arrow Operator

The arrow operator (->) provides a synonym for expressions involving the dot and dereference operators.

The dot operator fetches an element from an object of class type:

item1.same_isbn(item2); // run the same_isbn member of item1

If we had a pointer (or iterator) to a Sales_item, we would have to dereference the pointer (or iterator) before applying the dot operator. We must parenthesize the dereference because dereference has a lower precedence than dot.

Sales_item *sp = &item1;
(*sp).same_isbn(item2); // run same_isbn on object to which sp points

Better way:

Sales_item *sp = &item1;
sp->same_isbn(item2); // equivalent to (*sp).same_isbn(item2)
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.