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
What is static in oop?
Which language is not a true object oriented programming language?
What is polymorphism in oop example?
Why do we use inheritance?
What is the full form of oops?
What are benefits of oop?
What is the example of polymorphism?
Why do we need polymorphism in c#?
can we make game by using c
Why do we use oop?
What are the 3 principles of oop?
What is encapsulation process?
What is polymorphism what are the different types of polymorphism?
What is class and object in oops?
What is an interface in oop?