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