If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

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

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.

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:

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).

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.

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.

Re: Constructors in a singleton

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