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 does and I oop mean?
2. Give the different notations for the class.\
what is difference between class template and template class?
What is meant by multiple inheritance?
What is balance factor?
What is inheritance in simple words?
What is the fundamental idea of oop?
What is the difference between a mixin and inheritance?
what's the basic's in dot net
How do you achieve runtime polymorphism?
What are the features of oop?
Why is encapsulation used?
Whats oop mean?
what is the sylabus for priliminaries?
Write A Program to find the ambiguities in Multiple Inheritance? How are they resolved.(Virtual Functions)