#include #include #include "Counter_singleton.hpp" // Function declarations int main(); void increment_counter(); void decrement_counter(); // Function definitions int main() { // Accessing the singleton directly ++Counter::instance(); ++Counter::instance(); std::cout << *Counter::instance() << std::endl; // 2 // Initializing references to the singleton Counter &a{ Counter::instance() }; Counter &b{ Counter::instance() }; std::cout << *a << std::endl; // 2 std::cout << *b << std::endl; // 2 std::cout << *Counter::instance() << std::endl; // 2 ++a; ++b; std::cout << *a << std::endl; // 4 std::cout << *b << std::endl; // 4 std::cout << *Counter::instance() << std::endl; // 4 // Comparing the addresses of the objects std::cout << std::boolalpha; std::cout << ((&a == &b) && (&b == &Counter::instance())) << std::endl; // true // Accessing the value through the dereference operator Counter const &c{ Counter::instance() }; *a = 10; std::cout << *c << std::endl; // 10 *a = 4; std::cout << *c << std::endl; // 4 // Accessing the singleton from different functions increment_counter(); increment_counter(); increment_counter(); decrement_counter(); std::cout << *Counter::instance() << std::endl; // 6 // Additional tests of the postfix operators std::cout << a++ << std::endl; // 6 std::cout << *b << std::endl; // 7 std::cout << b-- << std::endl; // 7 std::cout << *c << std::endl; // 6 // Compilation-error tests: enable one statement at a time. // - Each one will cause a compile-time error // Counter direct{}; // Counter copied{ Counter::instance() }; // Counter moved{ std::move( a ) }; // a = b; // a = std::move( b ); // Counter::instance().~Counter(); // *c = 10; return 0; } void increment_counter() { ++Counter::instance(); } void decrement_counter() { --Counter::instance(); }