funptr2.cpp
492 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
using namespace std;
class X
{
protected:
int val;
public:
X(int v) : val(v) {};
virtual void f (double a)
{
cout << a + val <<endl;
}
virtual ~X(){};
};
class Y: public X
{
public:
Y(int v) : X(v) {};
void f (double a)
{
cout << 2 * a + val <<endl;
}
};
typedef void (X::*pf) (double); // pointer to member
void
test (X * p, X * q)
{
pf m = &X::f;
(p->*m)(6.0);
(q->*m)(7.0);
}
int
main ()
{
X i(3);
Y j(4);
test (&i, &j);
}