Wednesday, February 18, 2009

C++: Pointer to members

Pointer to member operators are not an everyday operators that we use but those can be useful if used in the correct way. Pointer to member operator ::* is used to declare a pointer that points to a member variable, pointer or function of a class. The operators .* and ->* are used to access it.

Following is an example of how we can use Pointer to member function (also called member function pointer).

  1 #include <iostream>
2 using namespace std;
3
4 class A{
5 public:
6 A(){
7 }
8 int Func(int a){
9 return a * 2;
10 }
11
12 };
13
14 int main(){
15 int (A::*fp)(int) = &A::Func;
16
17 A a;
18 cout << (a.*fp)(10) << endl;
19
20 return 0;
21 }
22

Following is another (almost random) example of how we can write generic classes to wrap pointer to member function to use with multiple classes.

  1 #include <iostream>
2 using namespace std;
3
4 class A{
5 public:
6 int Func1(int a){
7 return a * 2;
8 }
9
10 };
11
12 class B{
13 public:
14 int Func2(int a){
15 return a / 2;
16 }
17
18 };
19
20 template <class T>
21 class MemFnPtr
22 {
23 public:
24 MemFnPtr(int (T::*fp)(int), T &t) : m_fp(fp), m_t(t){
25 }
26 int Fn(int n)
27 {
28 return ((m_t).*(m_fp))(n);
29 }
30
31 //member vars
32 int (T::*m_fp)(int);
33 T &m_t;
34 };
35
36 int main(){
37 A a_obj;
38
39 MemFnPtr<A> fp_a(&A::Func1, a_obj);
40 cout << fp_a.Fn(10) << endl;
41
42 B b_obj;
43
44 MemFnPtr<B> fp_b(&B::Func2, b_obj);
45 cout << fp_b.Fn(10) << endl;
46
47 return 0;
48 }
49

C++ Problem: Define that macro

I'm planning to post some C++ programming problems that I come across and shall post their solutions too (I'll try to post multiple solutions). Here goes the first one:

Problem:

DO_REPORTING is MACRO which when called at the start of a function with appropriate parameter, prints the value of the parameter at the start of the function and also somehow prints the value before leaving the function. For the following code expected output would be:

Before:2
After:4
Before:4
After:2

How would you get this output without changing anything in class A and inside main function?
  1 #include <iostream>
2 using namespace std;
3
4 #define DO_REPORTING(type,variable) ;
5
6 class A{
7 protected:
8 int val;
9 public:
10 A(int v){
11 val = v;
12 }
13 void FuncMult(){
14 DO_REPORTING(int, val);
15 val *= 2;
16 }
17 void FuncDiv(){
18 DO_REPORTING(int, val);
19 val /= 2;
20 }
21 int GetVal(){
22 return val;
23 }
24 };
25
26 int main(){
27 A a(2);
28 a.FuncMult();
29 a.FuncDiv();
30 return 0;
31 }
32
33

Solution 1:
  1 #define DO_REPORTING(type,variable) Report<type> r(val);
2
3 template <class T>
4 class Report{
5 T &val;
6 public:
7 Report(T &v):val(v){
8 cout << "Before:" << val << endl;
9 }
10 ~Report(){
11 cout << "After:" << val << endl;
12 }
13 };
14