-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlyWeight.cpp
50 lines (41 loc) · 1014 Bytes
/
FlyWeight.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
/**
* \file FlyWeight.cpp
* \brief Flyweight creates objects as they are needed
*
* Because keeping them around is unnecessary.
* Here the Character class is a flyweight that is ONLY used for the duration of the inner loop
*/
#include <StdStream/StdStream.h>
#include <StdTest/StdTest.h>
#include <Stl.h>
//--------------------------------------------------------------------------------------------------
class Character
{
public:
explicit Character(const char c) :
_ch(c)
{
}
void output() const
{
std::cout << _ch;
}
private:
const char _ch {};
};
//--------------------------------------------------------------------------------------------------
int main()
{
const std::string msg = "Hello world!\n";
std::for_each(msg.cbegin(), msg.cend(),
[](const char a_ch) -> void
{
Character character(a_ch);
character.output();
});
return 0;
}
//--------------------------------------------------------------------------------------------------
#if OUTPUT
Hello world!
#endif