-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
63 lines (53 loc) · 1.32 KB
/
main.cpp
File metadata and controls
63 lines (53 loc) · 1.32 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
60
61
62
63
#include <iostream>
#include <vector>
#include <algorithm>
#include <memory>
using namespace std;
void print(const string& s)
{
cout << s << endl;
}
class Wrapper {
public:
template<typename T>
Wrapper(T object) : m_internal(make_unique<InternalImpl<T>>(object)) {}
friend void print(const Wrapper& wrapper)
{ wrapper.m_internal->print_(); }
private:
struct InternalInterface {
virtual ~InternalInterface() = default;
virtual void print_() const = 0;
};
template<typename T>
struct InternalImpl : InternalInterface
{
InternalImpl(T object) : m_object(object) {}
void print_() const override
{ print(m_object); }
private:
T m_object;
};
unique_ptr<InternalInterface> m_internal;
};
struct Person
{
Person(const string& firstname, const string& lastname)
: m_firstname(firstname)
, m_lastname(lastname)
{}
string m_firstname;
string m_lastname;
};
void print(const Person& p)
{
cout << "Firstname: " << p.m_firstname << ", lastname: " << p.m_lastname << endl;
}
int main()
{
vector<Wrapper> v;
v.emplace_back(string("I am using string class."));
v.emplace_back(Person("Laurent", "BERTHOLLE"));
for_each(v.begin(), v.end(),
[](const Wrapper& e) { print(e); }
);
}