C++ Singleton

All, Game & Software Engineering, Programming Languages
Singleton

A singleton is a design pattern that will allow only one instance of the object to exist during the execution of the program. The singleton is instantiated the first time the function Inst() is called and will remain “alive” for the rest of the program’s execution. Below is an example of various ways on how to use a singleton.

// MySingleton.cpp20

// Include the MySingleton library
#include "MySingleton.h"

// Declare and open the program
int main()
{
  // Print to the console "0 Hello World!"
  CMySingleton::Inst().Print();

  // Print to the console "1 Hello World!"
  CMySingleton::Inst().Print();

  // Fetch the instance of my singleton and
  // place it into the reference variable
  CMySingleton &clMySingleton = CMySingleton::Inst();

  // Print to the console "2 Hello World!"
  clMySingleton.Print();

  // Fetch the instance of my singleton and
  // place it into the reference variable
  CMySingleton &clMySingleton1 = CMySingleton::Inst();

  // Print to the console "3 Hello World!"
  clMySingleton1.Print();

  // Return to the 0.S. a good exit
  return(0);

} // End of int main()

Singleton print

When setting up a singleton one should declare the default constructor (and any other imaginable constructors) as private. This action will prevent accidental construction of a new instance of the object. Then one will also need to write out a function within the class that will return to the caller the instance of the singleton. The function will need to be a static function that will have within it a static instance of object; see the example below.

// MySingleton.h

// Include the standard input/output library
#include <stdio.h>

////////////////////////////////////////////////////////////////////////////////
// The class outlines a singleton that will print "Hello World!" to the console
class CMySingleton
{
public:
  ////////////////////////////////////////////////////////////////////////////
  // The function returns to the caller the instance of the singleton.
  static CMySingleton &Inst()
  {
  // Declare and define the single static instance of this object
  static CMySingleton clMe;

  // Return to the caller the single static instance of this object
  return(clMe);

  } // End of static CMySingleton &Inst()

  //////////////////////////////////////////////////////////////////////////////
  // The function will print to the console "Hello World!"
  void Print() { printf("%d Hello World!\n", iCount); iCount++; }

private:
  //////////////////////////////////////////////////////////////////////////////
  // Class Default Constructor.
  CMySingleton() : iCount(0) {}

  //////////////////////////////////////////////////////////////////////////////
  // Class Copy Constructor. There is not any implementation for this function
  // on purpose. There is no need for a copy constructor for a singleton.
  CMySingleton(const CMySingleton &amp;); // Don't implement.

  // Class counter
  int iCount;

}; // End of class CMySingleton
If you enjoyed our material please contribute! Or, please check out our affiliation with Amazon, and their current deals.

Please, keep your comments family friendly and respectful of each other and the author.