Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Problem in Singleton

Reply
Thread Tools

Problem in Singleton

 
 
Bangalore
Guest
Posts: n/a
 
      10-04-2005
Hi all
I am finding quite difficulty in understanding the behaviour of th
followin
program

Base class is singleton, so it should allow the creation of only on
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 addres
of variable i is 0x1003b010( and also in constructor of Base), but i
first and second cases it is different
I presume that,when i tried to create second and thir
object, compiler is not created those. I am not understanding why th
address o
i and k is changing

2. In case of Derived class , I haven't call constructor of derive
explicitly
But I know allocation of memory for the Derived class member has be
done
Cand anybody tell me how , this has been done

I know code snippet below is some what clumsy, plz bear with that
Code snippe


#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 ="<< &
<<"\t The address of k = "<< &k << endl
return i;
}

void Base :: Putdata()
{
cout <<"The address of i ="<< &
<<"\t The address of k = "<< &k << endl
i = 10;



//Static variabl
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 variabl
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 Objec
-----------------------\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 Objec
over--------------\n\n";

cout <<"Third objec
------------------------------\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

 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      10-04-2005
Bangalore wrote:
> 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.


Because you have different objects. You forgot to handle the copy
constructor and assignment operator, which are automatically created by the
compiler for your program.

This:

Base Base_Object= Base::Instance();

should actually not compile for a correctly implemented singleton class. It
makes a copy of your singleton instance, which of course should not be
possible.
You need to declare, but not define a private copy constructor and
assignment operator to prevent unintentional copying of your object.

 
Reply With Quote
 
 
 
 
Bangalore
Guest
Posts: n/a
 
      10-04-2005
> Rolf Magnuswrote:
Bangalore wrote:
> 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.
>

Because you have different objects. You forgot to handle the copy
constructor and assignment operator, which are automatically created
by the
compiler for your program.

This:

Base Base_Object= Base::Instance();

should actually not compile for a correctly implemented singleton
class. It
makes a copy of your singleton instance, which of course should not
be
possible.
You need to declare, but not define a private copy constructor and
assignment operator to prevent unintentional copying of your
object.[/quote:c75a7d7286]
Thanks a lot,

Can anybody explain my second question??

 
Reply With Quote
 
mlimber
Guest
Posts: n/a
 
      10-04-2005

Bangalore wrote:
> Hi all,
> 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

[snip]
> Derived & Derived::Instance()
> {
> if (_instance == NULL)
> _instance = static_cast < Derived *
> >(&(Base :: Instance()));

[snip]

This is very bad and the answer to your second question. See the FAQ:

http://www.parashift.com/c++-faq-lit....html#faq-21.2

Cheers! --M

 
Reply With Quote
 
mlimber
Guest
Posts: n/a
 
      10-04-2005
mlimber wrote:
> Bangalore wrote:
> > Hi all,
> > 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

> [snip]
> > Derived & Derived::Instance()
> > {
> > if (_instance == NULL)
> > _instance = static_cast < Derived *
> > >(&(Base :: Instance()));

> [snip]
>
> This is very bad and the answer to your second question. See the FAQ:
>
> http://www.parashift.com/c++-faq-lit....html#faq-21.2
>
> Cheers! --M


Wait a minute, that FAQ doesn't apply. Sorry. I didn't read it closely
enough. The problem is that you are casting from a base class to a
derived class. Ordinarily that can be accomplished with, not
static_cast, but dynamic_cast, which can fail, and in this case, the
dynamic_cast would certainly fail because the object returned from
Base::Instance is guaranteed not to be an instance of Derived. Consider
this code:

Derived *pD = static_cast<Derived*>(&(Base :: Instance()));
pD->j = 42;

You've told the compiler that the object pointed to by pD is a Derived
object, but in fact it is not. What will happen when Derived::j is
modified? It will write outside of its allocated memory, and undefined
behavior results.

Try this FAQ instead:

http://www.parashift.com/c++-faq-lit...html#faq-27.11

Cheers! --M

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Singleton methods without the singleton class Charles Oliver Nutter Ruby 4 03-22-2010 10:46 PM
Singleton object vs. enhancing singleton class Paul McMahon Ruby 3 06-09-2008 06:05 AM
Singleton Modules rather than Singleton Classes Trans Ruby 12 09-14-2007 06:45 AM
Singleton - Whether Cloneable overrides Singleton Proton Projects - Moin Java 4 03-27-2007 02:59 AM
Singleton classes and Singleton pattern Wilhelm Ruby 1 10-11-2006 01:08 PM



Advertisments