методы

 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
#include <iostream>
class Pointers {
public:
Pointers(int y)
: m_y(y)
{
/* пусто */
}
double test1(int x) {
return x * m_y;
}
double test2(int x) {
return x + m_y;
}
void run() {
std::cout << "Run: " << (this->*m_meth)(3) << std::endl;
}
void set_method(double (Pointers::*meth)(int x)) {
m_meth = meth;
}
private:
int m_y;
double (Pointers::*m_meth)(int x);
};
int main() {
Pointers p(3);
p.set_method(&Pointers::test1);
p.run();
p.set_method(&Pointers::test2);
p.run();
return 0;
}