-1

Possible Duplicate:
What is the arrow operator (->) synonym for in C++?

I just have a very basic question. Couldn't search it on google... I don't know what -> is and what it does in c++, for example:

render = Surface->Logic->Scale;
Community
  • 1
  • 1
Aistis
  • 251
  • 1
  • 2
  • 14
  • 3
    It is the arrow operator. [Possible Dublicate of this question:](http://stackoverflow.com/questions/221346/what-is-the-arrow-operator-synonym-for-in-c) – Özgür Aug 10 '11 at 11:40
  • Why -1? Did I forget to mention something? – Aistis Aug 10 '11 at 11:40
  • @Comptrol: You need to use `[description](link)` in the comments. – Cat Plus Plus Aug 10 '11 at 11:41
  • 3
    @net5tebiu - why the -neg rep (btw, I didn't do that). It's probably because the answer to your question is brutally and absolutely obvious in any C or C++ book, and either a) you are not reading your book, or b) you don't have a book (either case making your question a terrible and sloppy one.) – luis.espinal Aug 10 '11 at 11:42
  • @Cat Plus Plus, thanks for that info :)) – Özgür Aug 10 '11 at 11:46
  • I agree that this question is rather simple and could be solved with a google search but he did mention that he couldn't search it on google. Now my question is, if he couldn't find it on google or he couldn't use google to begin with. – Hallaghan Aug 10 '11 at 11:48
  • 2
    I'm learning C++ at school. It's not our main course. That's why we don't have any books. We didn't come to the point where we use this. But I've seen this operator(?) in many scripts and didn't know what it does. And those books are awfully expensive in my country... And I don't want to download illegal versions too... – Aistis Aug 10 '11 at 11:49
  • @Hallaghan I couldn't find it... – Aistis Aug 10 '11 at 11:49
  • 1
    @ne5tebiu, I get you man, it's not very straightforward when you're looking for operators like -> or @ in a language if you don't know what they are, at least. like @ in C# meaning a string literal, for e.g. And I do know some folks from Lithuania and I totally agree with you, books in your country are too expensive. But you can consult a number of free tutorials all over the internet which go through the basics and into some more complicated things. Have a look at [C++ Tutorial](http://www.cplusplus.com/doc/tutorial/) and read on a bit ;) – Hallaghan Aug 10 '11 at 11:53
  • @Hallaghan Thanks, I'll read that :) – Aistis Aug 10 '11 at 11:55
  • How can somebody be learning (or, worse, teaching) C++ without a book, even if it's not the main course. It's mind numbing. – luis.espinal Aug 10 '11 at 12:10
  • By the power on the internet maybe? :) – Aistis Aug 10 '11 at 12:22

4 Answers4

7

In C++ -> has 2 contexts:

  1. With respect to pointers
  2. With respect to objects

It dereferences the pointer, so basically it brings to memory location where the variable is stored at certain offset. Thus,

Surface->Logic->Scale;

is equivalent to,

(*Surface).Logic->Scale;  // same applies for 'Logic' too

With respect to object, you can mock this operator by overloading it as,

struct B { void foo(); };
struct A {
  B *p;
  B* operator -> () { return p; }
};

Usage will be,

A obj;
obj->foo();
iammilind
  • 68,093
  • 33
  • 169
  • 336
  • 1
    Note that, above is pseudo code, you may have to initialize `B *p;` to a valid object (like `p = new B;`). I haven't mentioned this, as it will complicate the demo. – iammilind Aug 10 '11 at 11:58
2

Do you have a book in C++? If the answer is no, then why not.

If the answer is yes, I'm sure that this is covered in it.

Anyways, the '->' symbol is the pointer de-referencing operator.

miken32
  • 42,008
  • 16
  • 111
  • 154
luis.espinal
  • 10,331
  • 6
  • 39
  • 55
2

The -> operator dereferences the pointer and accesses the member. It is similar to writing

(*(*Surface).Logic).Scale.

So in the above, we first deference the Surface pointer with

*Surface

Once the pointer is dereferenced we can access the Logic member

(*surface).Logic

Since Logic is also a pointer you need to dereference the pointer

*(*Surface).Logic

And now you can access the Scale member

(*(*Surface).Logic).Scale

As you can see this is much more cumbersome than using the -> operator. Which deferences the poiner and accesses the member in one.

Chris Taylor
  • 52,623
  • 10
  • 78
  • 89
2

Well, the operator arrow(->) in c++ is used to access the members of a struct using a pointer. for example say you have:

struct A {
int x;
};

A* mp  = new A;

then to access the element x and assign to some other element say y you have to write:

int y = mp -> x;

For further explanation you may look here:

http://www.cplusplus.com/doc/tutorial/structures/

A. K.
  • 34,395
  • 15
  • 52
  • 89