Added singleton example.

This commit is contained in:
Ian Dinwoodie
2019-04-29 20:06:03 -04:00
parent f9684d9c15
commit 2fd30aba50

View File

@@ -717,7 +717,39 @@ makes your code tightly coupled plus mocking the singleton could be difficult.
#### Programmatic Example
TODO
To create a singleton, make the constructor private, disable cloning, and create
a static variable to house the instance.
```cpp
class President
{
public:
static President& getInstance()
{
static President instance;
return instance;
}
private:
President()
{
}
~President()
{
}
};
```
Here is how this can be used:
```cpp
President& president1 = President::getInstance();
President& president2 = President::getInstance();
// There can only be 1 president, so they must be the same.
assert(&president1 == &president2);
```
#### When To Use