C++Syntax: Member selection: -> .
This syntax is used to select a
given a pointer to an object (->) or an object (.)
Selecting a data member means getting direct access to a members data, at most objects won't
allow that as it breaks the hiding
of its data. Selecting a member function means calling that function
(i.e. passing the object a
To pass a message to an object given
a pointer to it requires the -> operator:-
The operation involves two steps:-
- The pointer is followed to get to the object.
This is called
The syntax is:-
- The member function is selected. This step can also be performed using
the other member selection operator ., which works directly on the
Other Uses for member selection: -> .
None, Although . is also used as a decimal point of course!
An Alternative to ->
does not achieve the same result as the selection is
of higher priority so actually means:-
The correct syntax requires brackets to change the order:-
which is why the more transparent:-
- Treat objptr as an object and call its func.
- Take what results as a pointer and find the object
it points to.
It is not uncommon for an object to have a member function that returns
a pointer to another object. The returned object can then be sent
a sent a message. The example, suppose:-
then, because the -> operator associates left to right, the statement:-
objptr_a -> GetB() -> Print();
gets B from A and sends it the print message.
- objptr_a is a pointer to an object of class A the has a member
function call GetB which returns a pointer to an object of class B
- Class B has a member function Print.
Go Back to the
The C++ Crib Top Page
If you have any comments about this page please send them to