Pointers to Class Members : Pointer Primitive « Pointer « C++






Pointers to Class Members

Pointers to Class Members
 

#include <iostream>
using namespace std;
class MyClass {
public:
  MyClass(int i) { 
     val=i; 
  }
  int val;
  int doubleValue() { 
     return val+val; 
  }
};
int main()
{
  int MyClass::*data;                                 
  int (MyClass::*func)();                             
  MyClass myObject1(1), myObject2(2);                 
  data = &MyClass::val;                               
  func = &MyClass::doubleValue;
  cout << "Here are values: ";
  cout << myObject1.*data << " " << myObject2.*data << "\n";
  cout << "Here they are doubled: ";
  cout << (myObject1.*func)() << " ";
  cout << (myObject2.*func)() << "\n";
  return 0;
}


           
         
  








Related examples in the same category

1.Decrementing a Pointer for int valueDecrementing a Pointer for int value
2.Using a reference parameter for class typeUsing a reference parameter for class type
3.Assign the public object member address to a pointerAssign the public object member address to a pointer
4.Pointer as a VariablePointer as a Variable
5.Declares a pointer and then outputs its value without first assigning
6.Null PointersNull Pointers
7.How to use the address operator to assign the address of a variable to a pointer
8.Indirection Operator and DereferencingIndirection Operator and Dereferencing
9.The actual data type of the value of all pointers is the sameThe actual data type of the value of all pointers is the same
10.Prints the values and addresses of variables.Prints the values and addresses of variables.
11.Returning a pointer