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

Why do we learn c++?

727


In c++, what is the difference between method overloading and method overriding?

804


What is a pointer how and when is it used?

790


What is the function to call to turn an ascii string into a long?

814


What is virtual base class?

753


What is overloading unary operator?

811


What is string in c++ programming?

848


What is a singleton c++?

755


Is facebook written in c++?

747


What is the difference between the compiler and the preprocessor?

846


What are features of c++?

823


What is nested class in c++?

685


What is the oldest programming language?

767


What is enum class in c++?

931


Does dev c++ support c++ 11?

748