Item 15. Pointers to Class Members Are Not Pointers

It’s unfortunate that pointers to class members have the term “pointer” in their descriptions, because they don’t contain addresses and don’t behave like pointers.

The syntax for declaring a pointer to member is really not too horrible (if you’re already resigned to the declarator syntax for regular pointers):

int *ip; // pointer to an intint C::*pimC; // pointer to an int member of C

All you have to do is use classname::* rather than a plain * to indicate you’re referring to a member of classname. Otherwise, the syntax is the same as for a regular pointer declarator.

void *   *   *const* weird1;void *A::*B::*const* weird2;

The name weird1 has the type pointer to const pointer to pointer to ...

Get C++ Common Knowledge: Essential Intermediate Programming now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.