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 polymorphism explain its types?
Please send ford technologies placement paper 2 my mail id
Why do we use class?
Is oop better than procedural?
What type of loop is a for loop?
How to improve object oriented design skills?
What is a class in oop?
What is use of overloading?
How to handle exception in c++, For example in a functions i am assigning memory to some variables and also in next instructions in am dividing one variable also. If this functions generates a error while allocating memory to those variable and also while dividing the variable if i divide by zero then catch block how it will identify that this error has cone from perticular instruction
What is encapsulation in oops?
Can you name some types of inheritance?
Why is polymorphism needed?
What is encapsulation example?
Can you explain polymorphism?
What does and I oop mean?