singleton design

A

asit

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 ??
 
D

Dombo

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?
 
L

Lasse Reichstein Nielsen

asit said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top