From 1d73dd181e5747d171266815ece53c6fc241c10c Mon Sep 17 00:00:00 2001 From: Ian Dinwoodie Date: Mon, 29 Apr 2019 20:06:13 -0400 Subject: [PATCH] Added singleton example source code. --- examples/creational/singleton.cpp | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 examples/creational/singleton.cpp diff --git a/examples/creational/singleton.cpp b/examples/creational/singleton.cpp new file mode 100644 index 0000000..a409755 --- /dev/null +++ b/examples/creational/singleton.cpp @@ -0,0 +1,31 @@ +#include + +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; +}