Can we have a private constructor ?
Answer Posted / arun
#include<iostream>
using namespace std;
class Singleton
{
public:
static Singleton* Instance();
private:
static Singleton* pinstance;
Singleton();
};
Singleton* Singleton::pinstance = 0;// initialize pointer
Singleton* Singleton::Instance ()
{
if (pinstance == 0) // is it the first call?
{
pinstance = new Singleton; // create sole instance
}
return pinstance; // address of sole instance
}
Singleton::Singleton()
{
//... perform necessary instance initializations
}
void main()
{
Singleton *p1 = Singleton::Instance();
cout<<p1<<endl;
Singleton *p2 = p1->Instance();
cout<<p2<<endl;
Singleton & ref = * Singleton::Instance();
}
| Is This Answer Correct ? | 9 Yes | 4 No |
Post New Answer View All Answers
which feature are not hold visual basic of oop?
What is protected in oop?
What is the full form of oops?
Why do we need oop?
officer say me - i am offered to a smoking , then what can you say
Can we create object of abstract class?
What is the problem with multiple inheritance?
How do you explain polymorphism?
#include
What polymorphism means?
What is destructor example?
write a C++ program for booking using constructor and destructor.
What is the difference between a mixin and inheritance?
Give two or more real cenario of virtual function and vertual object
Describe these concepts: Polymorphism, Inheritance and Abstraction.