Thread: Guaranteeing only one instantiation of a class

  1. #1
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303

    Guaranteeing only one instantiation of a class

    The question I'm working on is: Create a class that represents a printer connection, and that only allows you to have one printer.

    Can someone give me a clue as to how to implement a class that limits the number of instantiations? I thought of having a class member which I can set to "false" if the static counter is already at 1, but this means the failed object nonetheless exists and seems messy since the client has to test the state of the object on their end. Better to have the object abort its own construction if there is already one in existence. But how do I go about doing this? Clue needed!

  2. #2
    Registered User
    Join Date
    Jun 2003
    Posts
    51
    I don't know is it the correct method to use.
    But you can always use a special constructor function which live outside the scope of the class.

    for example
    Code:
    TheObject & createTheObject()
    {
         static TheObject theobj = new TheObject();
    
         return theobj;
    }
    it will only create the object once no matter how many times you call the function

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Do a search for the Singleton Design Pattern.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  4. #4
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303
    Both good ideas, thanks! I'm reading about the Singleton pattern now. Although I'm not sure it's the solution the author of the book I'm using had in mind at this stage.

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I thought of having a class member which I can set to "false" if the static counter is already at 1, but this means the failed object nonetheless exists and seems messy since the client has to test the state of the object on their end.
    This might also work, but you could throw an exception: the failed object would not exist and the user wouldn't need to test object (just handle the errors).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. class composition constructor question...
    By andrea72 in forum C++ Programming
    Replies: 3
    Last Post: 04-03-2008, 05:11 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Creating a database
    By Shamino in forum Game Programming
    Replies: 19
    Last Post: 06-10-2007, 01:09 PM
  4. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM