Files
cpp-design-patterns-for-humans/examples/creational/singleton.cpp
2019-04-29 20:06:13 -04:00

32 lines
460 B
C++

#include <assert.h>
class President
{
public:
static President& getInstance()
{
static President instance;
return instance;
}
private:
President()
{
}
~President()
{
}
};
int main()
{
President& president1 = President::getInstance();
President& president2 = President::getInstance();
// There can only be 1 president, so they must be the same.
assert(&president1 == &president2);
return 0;
}