How I implemented:

#pragma once class Singletone { private: Singletone(): instance(nullptr) {} Singletone(const Singletone&); const Singletone& operator=(const Singletone&); static Singletone * instance; ~Singletone() { delete instance; } public: static const Singletone& getInstance(); } 


 #include "Singletone.h" Singletone* Singletone::instance = nullptr; const Singletone& Singletone::getInstance() { if(instance==nullptr) instance = new Singletone(); return *instance; } 

Tell me, how correct is this implementation?
For example, I can not return the link because copy constructor is forbidden (which should be forbidden, right?)
What then to do, return the pointer to the object, or? ...
And what is the best way to create single-threaded singleton in C ++?

  • > For example, I can not return the link> because copying constructor is forbidden> (which should be denied, right?) when creating links and pointers, the constructor is unnecessary (the object is not created) for details about returning links [How does the return link work? ] [1] [1]: - ProkletyiPirat
  • I can also recommend reading!topic/… (mostly due to comments on the topic). - Costantino Rupert
  • And it seems to me alone that the string ~ Singletone () {delete instance; } will call UB? If there is only one instance object, why at its removal. try to remove it again? - nnesterov
  • I probably do not understand something. In this case, the delete instance line will cause the ~Singletone() destructor, which again executes the delete instance line, which again will call the destructor, and so on. Or am I missing something? - nnesterov
  • 2
    @nnesterov Now once again I read the topstarter code and the ~Singletone() destructor will never be called at all, since the static member of the class is a raw pointer. Therefore, what you are saying will not happen, but the destructor is obviously meaningless. Sorry for misleading the previous comment :) - Costantino Rupert

3 answers 3


 if(instance==nullptr) instance = new Singletone(); 

And why did you decide that an uninitialized pointer would be nullptr ?


For example, I can not return the link because copy constructor is forbidden (which should be forbidden, right?)

And what is the relationship between the returned link and the copy constructor?

  • mikillskeg, please look again at my question, I added there that the .cpp instance is initialized with nullptr. 2. it cannot construct an object when it is returned. - PaulD
  • 2
    And how and where will the pointer be initiated if it is declared private? And if it is made public, the latter will be worse than the first. Better listen to the answer of @ KoVadim and learn the singleton Meyers. It looks like your decision, but not quite. Good luck. - skegg
  • one
    First, the pointer is declared static , i.e. it is guaranteed to be initially nullptr . Secondly, it is even explicitly initialized. - AnT

There are classic implementations of singletons, why not use it? At the same time learn typical errors:

  • thank . - PaulD

You have an extra destructor. In fact, you should not allow anyone to destroy singleton. The destructor must be left private, but empty.

I see no other problems in the single-threaded case.

To disable the copy constructor, assignment operator, and destructor, you can use the = delete construct, if your compiler supports C ++ 11. But it is a matter of taste.

You can return the link because the link to the object represents the same object, no copying takes place.

PS: in English like Singleton, without the "e".

ZZY: Yes, you still do not have a semicolon after the class declaration.

ZZZY: A singlelton Myers is much more elegant.