Can someone please remind me what "->" means and how to use it.

Recommended Answers

All 3 Replies

Hi,

That is a pointer arrow, it is used instead of 'dot' when dealing with pointers.
Example:

//Direct access with 'dot'
Object myObjectDirect;
myObjectDirect.doWork();

//Indirect access with pointer arrow
Object myObjectPointer = new Object();
myObjectPointer->doWork();

Notice the difference in the object declaration.

Regards,
Colezy

P.S. some people call it a 'dit' as 'pointer arrow' can be a mouthfull esspecially if you are describing a long command. The i in dit stands for indirect, so 'dit' is 'indrect dot'.

Hi,

I would like to add a bit to what Colezy said. First of all, I think there is a '*' missing in the code:

//Direct access with 'dot'
Object myObjectDirect;
myObjectDirect.doWork();

//Indirect access with pointer arrow
Object *myObjectPointer = new Object();
myObjectPointer->doWork();

To fully understand the arrow, consider this:
- To get the value a pointer refers to, we use the '*' operator.
- To get to a class member inside an object we use the '.' operator.
- The '.' operator has a higher priority than the '*' operator

So what we might intuitively think by simply using the '*' and '.' operators will not work:

Object *myObjectPointer = new Object();

//Wrong code
*myObjectPointer.doWork(); //Same as *(myObjectPointer.doWork());

Since the '.' operator has a higher priority, executing this means:
- Inside the object 'myObjectPointer' (not pointer), execute the function 'doWork()'
- 'doWork()' is expected to return a pointer value, which would then be used, by the '*' operator, to get the values referenced.

But we know this is wrong, because 'myObjectPointer' is the pointer not the object.

The simple (and more primitive) solution would be using brackets to shift the priority:

Object *myObjectPointer = new Object();

//Correct but unfriendly code
(*myObjectPointer).doWork();

Note that the '*' operator is inside the brakcets, it will work first, getting the object referenced by the pointer, then the function 'doWork()' will be looked up in that object.

But this is really annoying to keeping adding these brackets like that, and not so intuitively readable. This is where the operator '->' comes into action, as it is the equivelant of we just did with the brackets:

Object *myObjectPointer = new Object();

//Correct but unfriendly code
(*myObjectPointer).doWork();

//Correct and friendly code :)
myObjectPointer->doWork();

I hope this makes things clear rather than complicate them!

commented: Good post, clear information +1

I would like to add a bit to what Colezy said. First of all, I think there is a '*'

Cheers for pointing that out, yeah I somehow missed that.
Thats some good information there, good post.

Thanks,
Colezy

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.