C++ Pointers to members

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

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


Got any C++ Question?