Can someone please remind me what "->" means and how to use it.
That is a pointer arrow, it is used instead of 'dot' when dealing with pointers.
//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.
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'.
Edited 6 Years Ago by Colezy: n/a
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!
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.
Edited 6 Years Ago by Colezy: n/a
Hi. so this is actually a continuation from another question of mineHere but i was advised to start a new thread as the original question was already answered.
This is the result of previous question answered :
code for the listbox - datagridview interaction
At the top of the code ...
I have a 2d matrix with dimension (3, n) called A, I want to calculate the normalization and cross product of two arrays (b,z) (see the code please) for each column (for the first column, then the second one and so on).
the function that I created to find the ...
Hi. I have a form with list box : lst_product, datagridview : grd_order and button: btn_addline. lst_product has a list of product ids selected from database (MS Acess 2013) , grd_order is by default empty except for 2 headers and btn_addline adds rows to grd_order.
Private Sub btn_addline_Click(ByVal ...