Take the 2-minute tour ×

This question is fairly fundamental.I gave a simple and straighfoward test on my cygwin:

 class Example {
      Example(){ cout<<"dude..."<<endl; }
      ~Example(){ cout<<"see ya"<<endl; }
      static Example *GetInstance(){
          m_instance = new Example();
          cout<<"watch out bro"<<endl;
        return m_instance;
       void exp(){cout<<"greetings"<<endl;}
       static Example *m_instance;
 int main(){
   Example a;
   return 0;

Obviously,the output is:

 see ya

Technically singleton and typical constructor are pretty much different stories in c++ programming since singleton puts constructor as private while typical way is opposite.In my recent internship experiences I have noticed that most coders implement APIs in this manner.

I just wonder if that is the case or unnecessary when both class construction approaches exist in parallel.


Is constructor & singleton existing in one program practically nonsense cuz singleton stuff in this scope would become useless codes like unhazardous trash?

SUMMARY This is quite a nonsense question... and what's more,thanks to all of you brilliants constructor and singleton design pattern are "mutually exclusive" and in terms of vulnerbility,it is the same story as global variables kill our debug time...

share|improve this question
A "singleton" object which can have siblings (like this Example) is just a global variable, with all the noted downsides. –  MSalters Mar 5 '12 at 9:05
add comment

6 Answers

up vote 2 down vote accepted

By making the constructor of Example public, you allow users of your class to create an instance directly. If your design requires only one instance of your singleton then this allows users to inadvertently subvert this requirement. If you had made the constructor private then calling GetInstance() would be the only way to create an Example object, thus enforcing the requirement to allow only one instance to be created.

Note that static objects are destroyed in reverse order to which they were created. This causes a problem if the objects refer to each other. This is a trap for people maintaining your code, and if you have more than a few such objects this quickly becomes unmanageable.

Many developers consider singletons to be a Bad Thing much like global variables:


share|improve this answer
add comment

When using the Singleton design pattern you always should make the constructors (copy constructor as well) and the operator = as private to ensure there will be only one instance of the given class. Otherwise the Singleton pattern wouldn't make much sense.

share|improve this answer
add comment

I think that you missed the whole concept of singleton. Singleton means that there is only one instance, but public constructor can construct lot of objects.

share|improve this answer
is there any possibilities that both ways exist in one program?i've seen a lot of codes wrote in this manner so thats what baffles me –  jasonkim Mar 5 '12 at 8:54
you mean public constructor + 'singleton pattern' on one object? or some objects as singletons, and some as normal ones? –  Yossarian Mar 5 '12 at 8:55
@y26jin, of course they can exist in one program but then the pattern is broken. –  neciu Mar 5 '12 at 8:56
add comment

I think that example is broken. It allows user to create multiple "singletons" and then obviously they are no singletons.

In singleton constructor has to be private.

share|improve this answer
add comment

Just making the constructor private doesn't make the class Singleton. For that you have to make sure no other instances are present in the memory before you call the constructor. Even though the constructor is private any number of instances of the class can be created from static class members

share|improve this answer
add comment

Singleton is a technique to make sure there's only one instance to a class created. By exposing a public constructor you're obviously opening the door for multiple instances thus your class may not be called a singleton.

However it still has a static GetInstance() method that's somewhat expected as a singleton interface, and that makes it confusing to who didn't write the code (or even to the author some time later).

share|improve this answer
add comment

Your Answer


