-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample2.cpp
62 lines (46 loc) · 1.19 KB
/
example2.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
#include "out_of_line.hpp"
#include <iostream>
class Fast {
public:
char a = 'a', b = 'b';
};
class Slow {
public:
char c = 'c', d = 'd';
};
class Example1 : public out_of_line<Fast, Slow> {
public:
using out_of_line<Fast, Slow>::out_of_line;
char &get_c() {
return cold().c;
}
char const &get_c() const {
return cold().c;
}
char &get_d() {
return cold().d;
}
char const &get_d() const {
return cold().d;
}
int32_t a_plus_c() const {
return a + get_c();
}
};
class Example2 : public out_of_line<std::string, std::string> {
public:
using out_of_line<std::string, std::string>::out_of_line;
};
std::ostream &operator<<(std::ostream &os, Example2 const &obj) {
return os << static_cast<std::string const &>(obj) << ", " << obj.cold();
}
int main() {
std::cout << "Example1" << std::endl;
Example1 obj1({}, {});
std::cout << obj1.a << ", " << obj1.get_c() << ", " << obj1.a_plus_c() << std::endl;
obj1.get_c() = 'z';
std::cout << obj1.a_plus_c() << std::endl << std::endl;
std::cout << "Example2" << std::endl;
Example2 obj2("hot", "cold");
std::cout << obj2 << std::endl;
}