forked from ShujiaHuang/Cpp-Primer-Plus-6th
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemb_pt.cpp
78 lines (69 loc) · 1.47 KB
/
memb_pt.cpp
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// memb_pt.cpp -- dereferencing pointers to class members
#include <iostream>
using namespace std;
class Example
{
private:
int feet;
int inches;
public:
Example();
Example(int ft);
~Example();
void show_in() const;
void show_ft() const;
void use_ptr() const;
};
Example::Example()
{
feet = 0;
inches = 0;
}
Example::Example(int ft)
{
feet = ft;
inches = 12 * feet;
}
Example::~Example()
{
}
void Example::show_in() const
{
cout << inches << " inches\n";
}
void Example::show_ft() const
{
cout << feet << " feet\n";
}
void Example::use_ptr() const
{
Example yard(3);
int Example::*pt;
pt = &Example::inches;
cout << "Set pt to &Example::inches:\n";
cout << "this->pt: " << this->*pt << endl;
cout << "yard.*pt: " << yard.*pt << endl;
pt = &Example::feet;
cout << "Set pt to &Example::feet:\n";
cout << "this->pt: " << this->*pt << endl;
cout << "yard.*pt: " << yard.*pt << endl;
void (Example::*pf)() const;
pf = &Example::show_in;
cout << "Set pf to &Example::show_in:\n";
cout << "Using (this->*pf)(): ";
(this->*pf)();
cout << "Using (yard.*pf)(): ";
(yard.*pf)();
}
int main()
{
Example car(15);
Example van(20);
Example garage;
cout << "car.use_ptr() output:\n";
car.use_ptr();
cout << "\nvan.use_ptr() output:\n";
van.use_ptr();
cin.get();
return 0;
}