Stats

110 Contributors: 2 Saturday, August 6, 2016
Licensed under: CC-BY-SA
Not affiliated with Stack Overflow
Rip Tutorial: riptutorial@gmail.com
Roadmap: roadmap

Pointers to members

Syntax

  • Assuming a class named Class...

    • type *ptr = &Class::member; // Point to static members only
    • type Class::*ptr = &Class::member; // Point to non-static Class members
  • For pointers to non-static class members, given the following two definitions:

    • Class instance;
    • Class *p = &instance;
  • Pointers to Class member variables

    • ptr = &Class::i; // Point to variable i within every Class
    • instance.*ptr = 1; // Access instance's i
    • p->*ptr = 1; // Access p's i
  • Pointers to Class member functions

    • ptr = &Class::F; // Point to function 'F' within every Class
    • (instance.*ptr)(5); // Call instance's F
    • (p->*ptr)(6); // Call p's F

Related Examples