Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...

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


Please Help Members By Posting Answers For Below Questions

What does and I oop mean?

1104


2. Give the different notations for the class.\

2082


what is difference between class template and template class?

2596


What is meant by multiple inheritance?

1180


What is balance factor?

1018


What is inheritance in simple words?

1009


What is the fundamental idea of oop?

1081


What is the difference between a mixin and inheritance?

935


what's the basic's in dot net

2120


How do you achieve runtime polymorphism?

994


What are the features of oop?

1094


Why is encapsulation used?

947


Whats oop mean?

989


what is the sylabus for priliminaries?

2122


Write A Program to find the ambiguities in Multiple Inheritance? How are they resolved.(Virtual Functions)

4080