-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSample.cpp
More file actions
63 lines (53 loc) · 1.54 KB
/
Sample.cpp
File metadata and controls
63 lines (53 loc) · 1.54 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 <iomanip>
#include "Sample.h"
///////////////////////////////////////////////
// constructor
Sample::Sample():_a{999}
{
}
///////////////////////////////////////////////
// destructor (never called)
Sample::~Sample()
{
std::cout << "destroy" << std::endl;
}
///////////////////////////////////////////////
// setter
void Sample::set(int a)
{
_a = a;
}
///////////////////////////////////////////////
// getter
int Sample::get() const
{
return _a;
}
///////////////////////////////////////////////
// Test code for Singleton template class
Sample& func1()
{
Sample& s = Sample::getInstance();
std::cout << "func1=" << std::setw(3) << std::dec << s.get() << ", addr=" << std::hex << &s << std::endl;
s.set(100);
return s;
}
Sample& func2()
{
Sample& s = Sample::getInstance();
std::cout << "func2=" << std::setw(3) << std::dec << s.get() << ", addr=" << std::hex << &s << std::endl;
s.set(200);
return s;
}
int main()
{
auto& s1 = func1();
std::cout << " s1=" << std::setw(3) << std::dec << s1.get() << ", addr=" << std::hex << &s1 << std::endl;
auto& smain = Sample::getInstance();
std::cout << "smain=" << std::setw(3) << std::dec << smain.get() << ", addr=" << std::hex << &smain << std::endl;
smain.set(888);
std::cout << "smain=" << std::setw(3) << std::dec << smain.get() << ", addr=" << std::hex << &smain << std::endl;
auto& s2 = func2();
std::cout << " s2=" << std::setw(3) << std::dec << s2.get() << ", addr=" << std::hex << &s2 << std::endl;
}