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;
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;
In C++ ->
has 2 contexts:
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();
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.
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.
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: