Demonstrating operators .* and ->*. : Operators « Operators statements « C++ Tutorial






#include <iostream>
using std::cout;
using std::endl;

class MyClass
{
public:
   void myFunction()
   {
      cout << "In myFunction function\n";
   }

   int value;
};

void f( MyClass * );
void f2( MyClass * );

int main()
{
   MyClass myFunction;
   myFunction.value = 8;
   f( &myFunction );
   f2( &myFunction );
   return 0;
}

void f( MyClass *myFunctionPtr )
{
   void ( MyClass::*memPtr )() = &MyClass::myFunction;
   ( myFunctionPtr->*memPtr )();
}

void f2( MyClass *myFunctionPtr2 )
{
   int MyClass::*vPtr = &MyClass::value;
   cout << ( *myFunctionPtr2 ).*vPtr << endl;
}
In myFunction function
8








3.1.Operators
3.1.1.Precedence of C Operators
3.1.2.Demonstrating operators .* and ->*.
3.1.3.Using the unary scope resolution operator
3.1.4.Demonstrates built-in arithmetic operators
3.1.5.Using the & and * operators