-1

The reason for asking this rather "foolish" question is that I don't really know how to google it or how to search for it here on SO. The question is regarding the '->' operator, which can be used to access methods and variables from references to an object. Ex:

exClass* exObjPtr = new exClass;
exObjPtr -> exMethod();

I don't know what this (->) operator is called, and I can't google it or find it whiles searching on SO, because nether will alow searing for the special carachter '>'.

QUESTIONS:

  1. Where can I find more information about this topic?
  2. What is the operation called.
  3. From what I understand so far, the (->) operation is some sort of "syntactic sugar" for dereferncing an object and accessing it's members. What is the "non suger syntax" for this operation.

Apologize in advance for my rookie english! That you for your time!

m.s.
  • 16,063
  • 7
  • 53
  • 88
  • "which can be used to access methods and variables from references to an object." No, it is for access from *pointers*, not *references*. – juanchopanza Jun 18 '16 at 12:40
  • 2
    1) Off-topic 2) [Here](https://stackoverflow.com/questions/1580757/what-is-the-official-name-of-cs-arrow-operator) 3) [Here](https://stackoverflow.com/questions/221346/what-can-i-use-instead-of-the-arrow-operator) That's why you are supposed to post one question per question; now I don't know whether to close this as dupe of one of the above or vtc as off-topic. :/ (E: Thankfully, hvd got me out of this dilemma. :) ) – Baum mit Augen Jun 18 '16 at 12:43
  • More information can be found in any course on C++ (and C), really. The operator is often referred to as the "[arrow operator](http://stackoverflow.com/questions/1580757/what-is-the-official-name-of-cs-arrow-operator)". In C, `a->b` is sugar for `(*a).b`. In C++, most operators can be overridden, including `->`, which means that it shouldn't be called syntactic sugar. –  Jun 18 '16 at 12:44
  • @BaummitAugen The second question asks what the *operation* is called, not what the *operator* is called. Regardless, the first and second questions are also answered by the answers to the other question, which is why I felt it's okay to support the closing as a duplicate. –  Jun 18 '16 at 12:45
  • I'm sorry, I realize that this question probably have been asked like a milion times, but i didn't know how to search for it! – Mikael Henriksson Jun 18 '16 at 12:46
  • @hvd I guess the operation/operator thing is just a language mistake. Anyways, I agree with the dupe vote. – Baum mit Augen Jun 18 '16 at 12:47
  • @MikaelHenriksson Don't worry, close votes don't generally count against you. If people felt this was a poor question, they'd downvote it, that's something else and that hasn't happened (yet). –  Jun 18 '16 at 12:47
  • 1
    Mmm you can do more than searching though. How about looking up a [list of operators in C++ on Wikipedia](https://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B)? Research sometimes (often!) involves more than just putting a phrase into Google and hoping the answer is delivered to you: you have to _read_ and _study_! That's why I don't always buy the "I didn't know what to search for" reasoning... Anyway, hopefully this'll be a lesson and you'll be better at your research from now on. :) – Lightness Races in Orbit Jun 18 '16 at 14:59
  • @LightnessRacesinOrbit Thank you for the thinker buddy! I don't really know why I didn't think of that. It will be a lesson for likewise problems in the future! – Mikael Henriksson Jun 18 '16 at 16:29
  • @MikaelHenriksson: :) – Lightness Races in Orbit Jun 18 '16 at 17:44

2 Answers2

1

Arrow operator. It is used for accessing members of objects pointed to by pointers. Usually you would just use the dot:

Rectangle r;
r.length = 5;

But when the object is a pointer, we do

Rectangle* r = new Rectangle;
r->length = 5;

It is short form for

(*r).length = 5;
0

First, it is called pointers - Arrow operator (or for short, just arrow)

This operator works like this :

Type* a;
Type b;
a->method();
// same as (*a).method();
b.method();
Hearner
  • 2,711
  • 3
  • 17
  • 34