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
how to access grid view row?
What are the advantages of c++? Explain
How the memory management in vectors are being done. What happens when the heap memory is full, and how do you handle it ?
What is the difference between a definition and a declaration?
Write a Program for dynamically intialize a 2 dimentional array. Eg:5x20, accept strings and check for vowels and display the no.finally free the space allocated .
What do you understand by pure virtual function? Write about its use?
Explain 'this' pointer and what would happen if a pointer is deleted twice?
What are containers in c++?
Is it possible to provide special behavior for one instance of a template but not for other instances?
Should a constructor be public or private?
What is virtual base class?
What is iterator c++?
How many types of scopes are there in c++?
Define 'std'.
write a corrected statement so that the instruction will work properly. if (4 < x < 11) y = 2 * x;