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 multilevel inheritance?
What are oops methods?
class CTest { public: void someMethod() { int nCount = 0; cout << "This is some method --> " << nCount; } }; int main() { CTest *pctest; pctest->someMethod(); return 0; } It will executes the someMethod() and displays the value too. how is it possible with our creating memory for the class . i think iam not creating object for the class. Thanks in Advance... Prakash
write a program that takes input in digits and display the result in words from 1 to 1000
is there any choice in opting subjects like 4 out of 7
What is multilevel inheritance in oop?
Why do we use inheritance?
What is abstraction and encapsulation?
Why multiple inheritance is not possible?
What are the two different types of polymorphism?
IS IT NECESSARY TO INITIALIZE VARIABLE? WHAT IF THE INSTANCE VARIABLE IS DECLARED final ? IS IT NECESSARY TO INITIALIZE THE final VARIABLE AT THE TIME OF THEIR DECLARATION?
What is polymorphism and why is it important?
What are the 4 main oop principles?
What is interface? When and where is it used?
What is the renewal class?