funptr2.cpp
448 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
#include <iostream>
using namespace std;
class X
{
public:
virtual void f (double a)
{
cout << "X::f with parameter "<<a<<endl;
}
virtual ~X(){};
};
class Y: public X
{
public:
void f (double a)
{
cout << "Y::f with parameter "<<a<<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;
Y j;
test (&i, &j);
}