CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Threaded View

  1. #1
    Join Date
    Apr 2011
    Posts
    2

    Question Traditional Singleton Implementation vs Static Object Singleton Implementation

    I have usually implemented Singletons as described in the Design Patterns book. Recently someone suggested another implementation of Singleton using creating a static instance of a class and returning that instead of using new and returning a pointer to the new singleton. I would like to get your input. Is there any advantages of using one or the other? Disadvantages? Disadvantages for the static singleton implementation?

    Let me illustrate:

    Traditional Singleton Implementation

    Code:
    Car* Car::getInstance()
    {
       if(!InstanceFlag)
       {
          Instance = new Car();
    	  InstanceFlag = true;
    	  return Instance; 
       }		
       else
       {
    	  return mp_Instance;
       }
    }
    Static Implementation

    Code:
    Car& Car::getInstance()
    {
       static Car theCar;
       return theCar;
    }
    Advantages & Disadvantages? Any documents or articles I can refer to for the second implementation ? Thanks for your help!
    Last edited by ovidiucucu; April 7th, 2011 at 07:51 AM. Reason: added [CODE] tags

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured