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

Tell me can a pure virtual function have an implementation?

556


How can we read/write Structures from/to data files?

617


Function can be overloaded based on the parameter which is a value or a reference. Explain if the statement is true.

641


What does ctime() do?

614


What's the best free c++ profiler for windows?

618






What is constructor c++?

667


How does c++ structure differ from c++ class?

582


Which one is a preferred language C or C++? Why?

615


Write about an iterator class?

608


What is c++ 11 and c++ 14?

581


In what situations do you have to use initialization list rather than assignment in constructors?

631


What is iostream in c++ used for?

549


What is a node class in c++?

642


What does h mean in maths?

565


How do I use turbo c++?

547