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

Why do while loop is used?

1038


What are the benefits of interface?

1061


write a programe to calculate the simple intrest and compund intrest using by function overlading

2206


What is oops?what is its use in software engineering?

1034


Write a program to sort the number with different sorts in one program ??

2374


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?

2394


What is a class in oop?

1134


Why is polymorphism needed?

1070


What is Difeerence between List obj=new ArrayList(); and ArrayList obj=new ArrayList()?

2540


What is the benefit of oop?

1058


Why multiple inheritance is not allowed?

1159


What is encapsulation in ict?

1062


Which type does string inherit from?

1106


What does I oop mean?

1076


Get me a number puzzle game-program

2251