In C + +, pointers that can only point to class members can be used to extract data from this type of pointer, using the following two types of operators: the member object selection operator. * and member pointer selection operator->*
Example:
#include <iostream>using namespace std;struct c{ int x; float y; float Z;}; int main () { float F; int* i_ptr; C C1, C2; Float C::* f_ptr; A pointer to a float member of C f_ptr = &C::y; Point to C member y c1.*f_ptr = 3.14;//Set c1.y = 3.14 c2.*f_ptr = 2.01;//Set c2.y = 2.01 f_ptr = &am P C::z; Point to C member Z c1.*f_ptr = -9999.99,//set c1.z = -9999.99 f_ptr = &C::y;//point to c member y
c1.*f_ptr =-777.77; Set c1.y f_ptr = &C::x;//****error:x is not float**** f_ptr = &f;//****error:f are not a float m Ember of c**** i_ptr = &c1.x;//c1.x is an int //... return 0;}
Member selectors in C + +