What is the purpose of the following esoteric C++ operators?
Pointer to member
::*
Bind pointer to member by pointer
->*
Bind pointer to member by reference
.*
(reference)
A pointer to a member allows you to have a pointer that is relative to a specific class.
So, let's say you have a contact class with multiple phone numbers.
class contact
{
    phonenumber office;
    phonenumber home;
    phonenumber cell;
};
The idea is if you have an algorithm that needs to use a phone number but the decision of which phone number should be done outside the algorithm, pointers to member solve the problem:
void robocall(phonenumber contact::*number, ...);
Now the caller of robocall can decide which type of phonenumber to use:
robocall(&contact::home, ...);    // call home numbers
robocall(&contact::office, ...);  // call office number
.* and ->* come into play once you have a pointer.  So inside robocall, you would do:
contact c = ...;
c.*number;    // gets the appropriate phone number of the object
or:
contact *pc = ...;
pc->*number;
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With