3

Possible Duplicate:
Can any one provide me a sample of Singleton in c++?
C++ Singleton design pattern
C++ different singleton implementations

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.

Thanks in advance.

5
  • 1
    No. First you need a very good reason to use a singleton, and then you need another very good reason, and then we can talk about doing it. Commented Jul 29, 2012 at 11:01
  • 2
    Please avoid singletons. They're virtually always the wrong tool for the job. Read this Commented Jul 29, 2012 at 11:05
  • Search before ask: stackoverflow.com/questions/1008019/c-singleton-design-pattern Commented Jul 29, 2012 at 11:13
  • I'm sorry but I didn't found any answers to my question :) Commented Jul 29, 2012 at 11:19
  • See stackoverflow.com/a/1008289/14065 Commented Jul 29, 2012 at 14:36

2 Answers 2

4
//.h class MyClass { public: static MyClass &getInstance(); private: MyClass(); }; //.cpp MyClass & getInstance() { static MyClass instance; return instance; } 
Sign up to request clarification or add additional context in comments.

7 Comments

it works thanks and also @Roee Gavirel give the same answer
Nice. I always use the getInstance with the ==null. the use of the static member is simple yet smart.
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).
@JamesKanze can you expand on the order of destruction problem?
@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
|
2

Example:
logger.h:

#include <string> class Logger{ public: static Logger* Instance(); bool openLogFile(std::string logFile); void writeToLogFile(); bool closeLogFile(); private: 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; }; 

logger.c:

#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.. } 

more info in:

http://www.yolinux.com/TUTORIALS/C++Singleton.html

12 Comments

Linking to external resources is not a good idea. If that sites disappears then your answer will become useless to future readers.
@LokiAstari - You right. I've fix it.
And you need to find a way to delete that instance before program exits using atexit(..) or manually. I personally feel Andrew's answer is cleaner.
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.
@Thrustmaster Generally, you don't 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.)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.