singleton design

Discussion in 'C++' started by asit, Jul 31, 2011.

  1. asit

    asit Guest

    I want to design a singleton class. here the rule is to make the constructor private. That's okey. What about copy constructor and assignment operator.. If I make it private, certain things like copying and assigning will leadto compiler error. How can I handle them ??
     
    asit, Jul 31, 2011
    #1
    1. Advertising

  2. asit

    Dombo Guest

    Op 31-Jul-11 22:22, asit schreef:
    > I want to design a singleton class. here the rule is to make the
    > constructor private. That's okey. What about copy constructor
    > and assignment operator. If I make it private, certain things
    > like copying and assigning will lead to compiler error.
    > How can I handle them ??


    It wouldn't be really a singleton if you could copy it, nor would it
    make sense to support assignment if only one can exist, would it?
     
    Dombo, Jul 31, 2011
    #2
    1. Advertising

  3. asit <> writes:

    > I want to design a singleton class.


    Ick. Please reconsider.

    There is usually no good reason to enforce that there is only one
    instance of a class. Modelling-wise it might make sense to only
    actually create one instance, but using the singleton (anti-)pattern
    tends to bite you if you want to test either the class itself or
    classes depending on it.

    > here the rule is to make the constructor private. That's okey. What
    > about copy constructor and assignment operator. If I make it
    > private, certain things like copying and assigning will lead to
    > compiler error. How can I handle them ??


    If you want to only have one copy, you don't want to have assignment
    or copying creating more than one instance. If the object is immutable,
    it might not make any difference, but it still prevents it from being
    a singleton.
    You should only pass the object around by reference or pointer.

    /L
    --
    Lasse Reichstein Holst Nielsen
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Aug 1, 2011
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Proton Projects - Moin

    Singleton - Whether Cloneable overrides Singleton

    Proton Projects - Moin, Mar 26, 2007, in forum: Java
    Replies:
    4
    Views:
    3,259
    Proton Projects - Moin
    Mar 27, 2007
  2. Wilhelm
    Replies:
    1
    Views:
    167
  3. Trans
    Replies:
    12
    Views:
    280
    Robert Klemme
    Sep 14, 2007
  4. Paul McMahon
    Replies:
    3
    Views:
    207
    David A. Black
    Jun 9, 2008
  5. Charles Oliver Nutter

    Singleton methods without the singleton class

    Charles Oliver Nutter, Mar 15, 2010, in forum: Ruby
    Replies:
    4
    Views:
    204
    Charles Oliver Nutter
    Mar 22, 2010
Loading...

Share This Page