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
Why do while loop is used?
What are the benefits of interface?
write a programe to calculate the simple intrest and compund intrest using by function overlading
What is oops?what is its use in software engineering?
Write a program to sort the number with different sorts in one program ??
String = "C++ is an object oriented programming language.An imp feature of oops is classes and objects".Write a pgm to count the repeated words from this scenario?
What is a class in oop?
Why is polymorphism needed?
What is Difeerence between List obj=new ArrayList(); and ArrayList obj=new ArrayList()?
What is the benefit of oop?
Why multiple inheritance is not allowed?
What is encapsulation in ict?
Which type does string inherit from?
What does I oop mean?
Get me a number puzzle game-program