CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Apr 2013
    Posts
    5

    Constructors in a singleton

    Hi all,

    I need to implement a singleton, so I've been reading about it online and I'm still not quite sure about all the types of constructors I need to declare:

    Code:
    class my_singleton {
    	private:
    		my_singleton();
    		my_singleton(my_singleton & X);
    		my_singleton(const my_singleton & X);
    		...
    		static bool instance;
    		...
    	public:
    		static my_singleton* get_instance();
    		...
    		~my_singleton();
    }
    Is this OK?

    Thanks.

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Constructors in a singleton

    Why do you want to have a const and a non-const overload of your copy constructor? I'm not even sure that will compile, but I certainly can't see how it could be useful.
    For that matter, why do you want to copy your singleton? That goes against the nature of a singleton.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  3. #3
    Join Date
    Apr 2013
    Posts
    5

    Re: Constructors in a singleton

    Well from what I read, if I will not define a private copy constructor a default one will be automatically created and multiple copies of my singleton could be made via it.
    So I defined a private one, but I am not sure whether I should define a const one or a non-const one so as to satisfy the compiler so it will not create a default one.

    What do you think?

    Thanks.

  4. #4
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Constructors in a singleton

    Quote Originally Posted by tkks View Post
    Well from what I read, if I will not define a private copy constructor a default one will be automatically created and multiple copies of my singleton could be made via it.
    So I defined a private one, but I am not sure whether I should define a const one or a non-const one so as to satisfy the compiler so it will not create a default one.
    Well, you can easily test it by attempting to copy your singleton and see if you get a compiler error or linker error. If you do get either one, then the private one is sufficient.

    All you need is the one you have provided, but you did not provide a private assignment operator (operator =). Your singleton can still be copied using operator = if you do not provide a private, unimplemented version of this operator.

    Also, you need to think about this:
    Code:
    ~my_singleton();
    It is now public, meaning the client can destroy the singleton at any time. Is this desired, as construction is private? Shouldn't destruction also be private?

    Usually it is the singleton that controls its own lifetime (there are implementations that will clean up themselves when an app closes down, but that's more advanced).

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; April 7th, 2013 at 01:00 AM.

  5. #5
    Join Date
    Apr 2013
    Posts
    5

    Re: Constructors in a singleton

    Now given that definition (whatever it is), does it make sense to have multiple copies?
    Multiple copies of what? of the singleton? then no that is the whole reason I want to make all the constructors private.
    So that the compiler won't make default constructors thus allowing people to make more then one copy of my singleton.

    I want to control the creation of the singleton instances, so as to limit the number of instances to one! but the compiler will create a default constructor and a default copy constructor thus allowing someone to create as many copies as he wants. That is why I want to define private constructors so nobody would be able to create more then one instance of the singleton.

    I am not sure tho whether I need a const one and a non-const one.

    Thanks.

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Constructors in a singleton

    Quote Originally Posted by tkks View Post
    Multiple copies of what? of the singleton? then no that is the whole reason I want to make all the constructors private.
    So that the compiler won't make default constructors thus allowing people to make more then one copy of my singleton.
    See my updated response.

    All you need is to define one, and you need to define an assignment operator that is private. Also, your implementation allows public destruction, which means that your singleton can be a "zeroton", and I don't think you want that to happen.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; April 6th, 2013 at 07:04 PM.

  7. #7
    Join Date
    Apr 2013
    Posts
    5

    Re: Constructors in a singleton

    Oh, I see.

    Thanks a lot, that helped me!

  8. #8
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Constructors in a singleton

    Quote Originally Posted by tkks View Post
    Well from what I read, if I will not define a private copy constructor a default one will be automatically created and multiple copies of my singleton could be made via it.
    So I defined a private one, but I am not sure whether I should define a const one or a non-const one so as to satisfy the compiler so it will not create a default one.

    What do you think?
    If you want to avoid the default generated copy constructor (or copy assignment operator), all you need to do is declare it private and do not define it. This way you will get either an access violation error or a linker error when you try to use them somewhere in your code. You did not show the function definitions in your OP, so I assumed all those functions were implemented. Newer compilers also support explicitly deleting the default generated functions, see http://en.wikipedia.org/wiki/C%2B%2B...mber_functions.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  9. #9
    Join Date
    Apr 2013
    Posts
    5

    Re: Constructors in a singleton

    Newer compilers also support explicitly deleting the default generated functions, see http://en.wikipedia.org/wiki/C%2B%2B...mber_functions.
    Wow, didn't know that. Thanks!

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