forked from SilverMaple/STLSourceCodeNote
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path8_4_5_mem-fun-test.cpp
More file actions
54 lines (45 loc) · 1001 Bytes
/
8_4_5_mem-fun-test.cpp
File metadata and controls
54 lines (45 loc) · 1001 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
45
46
47
48
49
50
51
52
53
54
// file: mem-fun-test.cpp
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
class Shape {
public:
virtual void display() = 0;
};
class Rect : public Shape {
public:
virtual void display() {
cout << "Rect: ";
}
};
class Circle : public Shape {
public:
virtual void display() {
cout << "Circle: ";
}
};
class Square : public Shape {
public:
virtual void display() {
cout << "Square: ";
}
};
int main() {
// STL容器只支持value semantic,不支持reference semantics
// vector<Shape&> V; 无法通过编译
vector<Shape *> V;
V.push_back(new Rect);
V.push_back(new Circle);
V.push_back(new Square);
V.push_back(new Circle);
V.push_back(new Rect);
// polymorphically
for (int i = 0; i < V.size(); ++i)
V[i]->display();
cout << endl;
// polymorphically
for_each(V.begin(), V.end(), mem_fun(&Shape::display));
cout << endl;
}