Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Esoteric C++ operators

Tags:

c++

operators

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)

like image 353
Neil G Avatar asked May 10 '10 22:05

Neil G


1 Answers

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;
like image 159
R Samuel Klatchko Avatar answered Oct 05 '22 04:10

R Samuel Klatchko