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.
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.
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.
Re: Constructors in a singleton
Quote:
Originally Posted by
tkks
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:
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
Re: Constructors in a singleton
Quote:
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.
Re: Constructors in a singleton
Quote:
Originally Posted by
tkks
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
Re: Constructors in a singleton
Oh, I see.
Thanks a lot, that helped me!
Re: Constructors in a singleton
Quote:
Originally Posted by
tkks
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.
Re: Constructors in a singleton
Wow, didn't know that. Thanks!