-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvirtualize_construcotor_and_non_member_function.cc
More file actions
59 lines (50 loc) · 1.28 KB
/
virtualize_construcotor_and_non_member_function.cc
File metadata and controls
59 lines (50 loc) · 1.28 KB
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
#include <list>
#include <iostream>
class NLComponent {
public:
// virtual copy constructor
virtual NLComponent* clone() const = 0;
virtual std::ostream& print(std::ostream& os) const = 0;
};
class TextBlock: public NLComponent {
public:
virtual TextBlock* clone() const {
return new TextBlock(*this);
}
virtual std::ostream& print(std::ostream& os) const;
};
class Graphic: public NLComponent {
public:
virtual Graphic* clone() const {
return new Graphic(*this);
}
virtual std::ostream& print(std::ostream& os) const;
};
class NewsLetter {
public:
NewsLetter(std::istream& is);
NewsLetter(const NewsLetter& rhs);
private:
static NLComponent* ReadComponent(std::istream& is);
std::list<NLComponent*> components;
};
NewsLetter::NewsLetter(std::istream& is) {
while (is) {
components.push_back(ReadComponent(is));
}
}
NewsLetter::NewsLetter(const NewsLetter& rhs) {
for (auto it = rhs.components.cbegin(); it != rhs.components.cend(); ++it) {
components.push_back((*it)->clone());
}
}
NLComponent* NewsLetter::ReadComponent(std::istream& is) {
// read the next component object from is
return nullptr; // just for test
}
inline std::ostream& operator<<(std::ostream& os, const NLComponent& c) {
return c.print(os);
}
int main() {
return 0;
}