-
April 6th, 2013, 12:40 PM
#1
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.
-
April 6th, 2013, 04:34 PM
#2
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
-
April 6th, 2013, 05:40 PM
#3
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.
-
April 6th, 2013, 06:39 PM
#4
Re: Constructors in a singleton
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
Last edited by Paul McKenzie; April 7th, 2013 at 01:00 AM.
-
April 6th, 2013, 06:45 PM
#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.
-
April 6th, 2013, 07:02 PM
#6
Re: Constructors in a singleton
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
Last edited by Paul McKenzie; April 6th, 2013 at 07:04 PM.
-
April 6th, 2013, 10:38 PM
#7
Re: Constructors in a singleton
Oh, I see.
Thanks a lot, that helped me!
-
April 7th, 2013, 05:56 AM
#8
Re: Constructors in a singleton
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.
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
-
April 7th, 2013, 01:41 PM
#9
Re: Constructors in a singleton
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|