Singleton class

E

ernaveenkoul

hi,

Hope i get some answers.

What is single ton class?how to make singleton class?use of single ton
class.

Regards

Naveen
 
M

md

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

Marcelo Pinto

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
 

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
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top