C ++ Clase Singleton getInstance (como java) [duplicado]

Posible duplicado:
¿Alguien puede proporcionarme una muestra de Singleton en c ++?
Patrón de diseño C ++ Singleton
C ++ diferentes implementaciones de singleton

I need some example of Singleton in c++ class because i have never wrote such class. For an example in java I can declare d a static field which is private and it's initialize in the constructor and a method getInstance which is also static and return the already initialize field instance.

Gracias de antemano.

preguntado el 29 de julio de 12 a las 11:07

No. First you need a very good reason to use a singleton, and then you need another very good reason, and luego we can talk about doing it. -

Please avoid singletons. They're virtually always the wrong tool for the job. Lee esto -

I'm sorry but I didn't found any answers to my question :) -

2 Respuestas

class MyClass
    static MyClass &getInstance();


MyClass & getInstance()
    static MyClass instance;
    return instance;

Respondido 29 Jul 12, 11:07

it works thanks and also @Roee Gavirel give the same answer - jordan borisov

Nice. I always use the getInstance con el ==null. the use of the static member is simple yet smart. - Roee Gavirel

One potential problem with this is that the instance will be destructed at some unknown point in time, possibly causing an order of destruction problem. I use this solution if the singleton needs destruction, but the pointer solution if it doesn't (which is well over 90% of the time). - james kanze

@JamesKanze can you expand on the order of destruction ¿problema? - quamrana

@quamrana: an order for destructing static variables is not specified. So if you have 2 singletons for example it's not defined which one will be destructed first - Andrés


#include <string>

class Logger{
   static Logger* Instance();
   bool openLogFile(std::string logFile);
   void writeToLogFile();
   bool closeLogFile();

   Logger(){};  // Private so that it can  not be called
   Logger(Logger const&){};             // copy constructor is private
   Logger& operator=(Logger const&){};  // assignment operator is private
   static Logger* m_pInstance;


#include "logger.h"

// Global static pointer used to ensure a single instance of the class.
Logger* Logger::m_pInstance = NULL; 

/** This function is called to create an instance of the class.
    Calling the constructor publicly is not allowed. The constructor
    is private and is only called by this Instance function.

Logger* Logger::Instance()
   if (!m_pInstance)   // Only allow one instance of class to be generated.
      m_pInstance = new Logger;

   return m_pInstance;

bool Logger::openLogFile(std::string _logFile)
    //Your code..

más info en:


Respondido 29 Jul 12, 15:07

Linking to external resources is not a good idea. If that sites disappears then your answer will become useless to future readers. - martín york

@LokiAstari - You right. I've fix it. - Roee Gavirel

And you need to find a way to delete that instance before program exits using atexit(..) or manually. I personally feel La respuesta de Andrew es más limpio. - UltraInstinto

Now that it is here. That is a not a good singleton. No concept of ownership. Thus no way of knowing when to delete it, nor is destruction automatic. Also because you are returning a pointer you are forcing the user to check for null. - martín york

@Thrustmaster Generally, you no want to delete a singleton. You're using it to solve order of initialization problems; deleting it will introduce order of destruction problems. (There are exceptions, of course, where you have to delete it.) - james kanze

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.