I am finding quite difficulty in understanding the behaviour of the
following
program.
Base class is singleton, so it should allow the creation of only one
object.
Eventhough it is singleton , i tried to create three objects from it,
and I succeeded with only one object with address "0x1003b010"
Problem:
1. I am not understanding the third object behaviour, here the address
of variable i is 0x1003b010( and also in constructor of Base), but in
first and second cases it is different,
I presume that,when i tried to create second and third
object, compiler is not created those. I am not understanding why the
address of
i and k is changing.
2. In case of Derived class , I haven't call constructor of derived
explicitly.
But I know allocation of memory for the Derived class member has bee
done.
Cand anybody tell me how , this has been done.
I know code snippet below is some what clumsy, plz bear with that.
Code snippet
#include<iostream>
using namespace std;
class Base
{
private:
//static Base *_instance;
int i,k;
protected:
Base()
{
cout <<"Calling Base Constructor \n";
cout <<"The address of i = " << &i << endl;
cout <<"The address of k = " << &k << endl;
cout <<"Construction of Base class over \n";
}
public:
static Base *_instance;
static Base& Instance();
int Getdata();
void Putdata();
};
Base& Base :: Instance()
{
if (_instance == NULL)
_instance = new Base();
return *_instance;
}
int Base :: Getdata()
{
cout <<"The address of i ="<< &i
<<"\t The address of k = "<< &k << endl;
return i;
}
void Base :: Putdata()
{
cout <<"The address of i ="<< &i
<<"\t The address of k = "<< &k << endl;
i = 10;
}
//Static variable
Base *Base::_instance = NULL;
class Derived : public Base
{
private:
int j;
int *ptr;
static Derived *_instance;
protected:
Derived()
{
ptr = new int;
*ptr=100;
cout <<"In Constructor "<<
*ptr <<endl;
}
public:
static Derived & Instance();
int Getdataderv();
void Putdataderv();
};
Derived & Derived::Instance()
{
if (_instance == NULL)
_instance = static_cast < Derived *
(&(Base :: Instance()));
return *_instance;
}
int Derived :: Getdataderv()
{
return j;
}
void Derived :: Putdataderv()
{
j = 20;
}
//Static variable
Derived *Derived::_instance = NULL;
int main()
{
cout <<"The value of Base::_instance = " <<
(Base::_instance);
cout << "The First Object -----------------------\n";
Base Base_Object= Base::Instance();
cout <<"The value of Base::_instance = " <<
(Base::_instance);
Base_Object.Putdata();
Base_Object.Getdata();
cout << "The address of Base_Object = " <<
&Base_Object << endl;
cout << "Work with First Object over--------------\n\n";
cout << "The Second Object
-----------------------\n";
Base Base_Object_2= Base::Instance();
cout <<"The value of Base::_instance = " <<
(Base::_instance);
Base_Object_2.Putdata();
Base_Object_2.Getdata();
cout << "The address of Base_Object = " <<
&Base_Object_2 << endl;
cout << "Work with Second Object
over--------------\n\n";
cout <<"Third object
------------------------------\n";
Base::Instance().Putdata();
cout<<Base::Instance().Getdata()<<endl;
cout<<&(Base::Instance())<<endl;
cout <<"Third object over
--------------------------\n\n";
cout <<"Derived Object ----------------------------\n";
Derived::Instance().Putdataderv();
cout<<Derived::Instance().Getdataderv()<<endl;
cout<<&(Derived::Instance())<<endl;
cout <<"Derived Object over
------------------------\n\n";
}
output:
The value of Base::_instance = 0x0The First Object
-----------------------
Calling Base Constructor
The address of i = 0x1003b010
The address of k = 0x1003b014
Construction of Base class over
The value of Base::_instance = 0x1003b010The address of i =0x7ffd8b00
The address of k = 0x7ffd8b04
The address of i =0x7ffd8b00 The address of k = 0x7ffd8b04
The address of Base_Object = 0x7ffd8b00
Work with First Object over--------------
The Second Object -----------------------
The value of Base::_instance = 0x1003b010The address of i =0x7ffd8b08
The address of k = 0x7ffd8b0c
The address of i =0x7ffd8b08 The address of k = 0x7ffd8b0c
The address of Base_Object = 0x7ffd8b08
Work with Second Object over--------------
Third object ------------------------------
The address of i =0x1003b010 The address of k = 0x1003b014
The address of i =0x1003b010 The address of k = 0x1003b014
10
0x1003b010
Third object over --------------------------
Derived Object ----------------------------
20
0x1003b010
Derived Object over ------------------------
Thanks,
Bangalore