-
Notifications
You must be signed in to change notification settings - Fork 0
/
constructor.cpp
68 lines (59 loc) · 1.52 KB
/
constructor.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
// implicit copy constructor demo
// this is why it's good to explicitly delete the copy constructor and operator=
// unless you intend to use that behavior
#include <iostream>
enum class Environment { kDevelopment, kStaging, kProduction };
void print_env(Environment e) {
switch (e) {
case Environment::kDevelopment:
std::cout << "dev";
break;
case Environment::kStaging:
std::cout << "staging";
break;
case Environment::kProduction:
std::cout << "prod";
break;
}
}
struct SampleParam {
// NOTE: default environment when this runs
Environment environment = Environment::kProduction;
SampleParam() {
std::cout << "SampleParam(";
print_env(environment);
std::cout << ")" << std::endl;
}
SampleParam(const SampleParam& copy) {
environment = copy.environment;
std::cout << "SampleParam_COPY(";
print_env(environment);
std::cout << ")" << std::endl;
}
~SampleParam() {
std::cout << "~SampleParam" << std::endl;
}
};
SampleParam GetParam(Environment e) {
SampleParam o;
o.environment = e;
return o;
}
class SampleObject {
public:
SampleParam _options;
SampleObject(Environment e) : _options(GetParam(e)) {
std::cout << "SampleObject()" << std::endl;
}
virtual ~SampleObject() {
std::cout << "~SampleObject()" << std::endl;
}
};
int constructor_main(int argc, wchar_t* argv[]) {
std::cout << std::endl << __FILE__ << std::endl;
SampleObject abc(Environment::kDevelopment);
std::cout << "printing instance param value: ";
print_env(abc._options.environment);
std::cout << std::endl;
return 0;
}