md escreveu:
when we create a class with constructor in private, then the class is
singleton class.
Purpose is when u don't want any body to create a object of ur class
then place constructor in private.
Note quite. A singleton is a class that allows only one instance to be
created. A singleton must have private constructors and destructor in
order to disallow arbitrary creation objects, but the fact that a class
has private constructors and destructors doesn't guarantee that it is a
singleton:
class nosingleton
{
public:
//may leak
nosingleton * create() { new nosingleton; }
private:
nosingleton() {}
nosingleton(const nosingleton &); //not implemented
nosingleton & operator = (const nosinlgeton &); //not implemented
};
Each time you call create() you get a new object of type nosingleton.
And, in addition, if you define the destructor of nosingleton private,
you definitely will have memory leaks.
HTH,
Marcelo Pinto