How to avoid a class from instantiation?

Answer Posted / maniac_2004

if you want to construct a class whos constructor is private
then use a static method which constructs the object and
returns the pointer

class A
{
private: A();
public:
static A * createInstance();
};

A* A::getInstance()
{
return new A();
}

main(){ A::createInstance();}

This is similar to the "singleton" pattern...

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What are the advantages of using a pointer?

609


What gives the current position of the put pointer?

565


Write down the equivalent pointer expression for referring the same element a[i][j][k][l]?

707


Explain what you mean by a pointer.

636


What is difference between class and function?

585






What does override mean in c++?

592


What is the return value of the insertion operator?

606


Is c++ slower than c?

577


What are references in c++? What is a local reference?

576


What is the use of main function in c++?

531


What is abstraction with real time example?

623


Is turbo c++ free?

625


Is sorted c++?

580


Which recursive sorting technique always makes recursive calls to sort subarrays that are about half size of the original array?

614


Specify some guidelines that should be followed while overloading operators?

625