C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

singleton classes.

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
krish
Guest





PostPosted: Mon Dec 05, 2005 10:11 am    Post subject: singleton classes. Reply with quote



I have a problem with the singleton classes...
I have a main controller class like the following

class mainctrl
{
public:
void initialize();
void getvalue();
};

and a singleton class
class subctrl
{
subctrl();
public:
getsubvalue;
~subctrl();
static subctrl& getinstance();
};

and the definitionof the getInstance function is as follows.

subctrl& subctrl::getInstance()
{
static subctrl instance;
return instance;
}



now both the functions of main controller should be using the functions
of subctrl class.
so am using the singleton class as follows.
mainctrl::initialize()
{
subctrl &ctrl = subctrl::getInstance();
ctrl.getsubvalue();
}

mainctrl::getvalue()
{
subctrl &ctrl = subctrl::getInstance();
ctrl.getsubvalue();
}
since the object's instance is static it gives me the same result
whereever i am creating the reference.

My question is instead of declaring it local to every function and
using it, is there anyway that i can create a reference of this object
globally somewhere inside the class declaration and use it across all
my functions???
it wud be great if anyone throws some light on this area..


regards
krish


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Carl Barron
Guest





PostPosted: Mon Dec 05, 2005 12:27 pm    Post subject: Re: singleton classes. Reply with quote



In article <1133769923.777075.112770 (AT) g49g2000cwa (DOT) googlegroups.com>,
krish <krishnakanna (AT) rediffmail (DOT) com> wrote:

Quote:
I have a problem with the singleton classes...
I have a main controller class like the following

class mainctrl
{
public:
void initialize();
void getvalue();
};

and a singleton class
class subctrl
{
subctrl();
public:
getsubvalue;
~subctrl();
static subctrl& getinstance();
};

since the object's instance is static it gives me the same result
whereever i am creating the reference.

My question is instead of declaring it local to every function and
using it, is there anyway that i can create a reference of this object
globally somewhere inside the class declaration and use it across all
my functions???
it wud be great if anyone throws some light on this area..

either hold it in another struct like

struct singleton_holder
{
subctr &sub_control;
singleton_holder() :sub_control(subctrl.get_instance()){}
};
singleton_holder &global;
access via global.sub_control.

or use a pointer
subctrl *p_controller;

int main()
{
p_controller = &subctrl::get_instance();
//...
}
access as a pointer rather than a reference. could be dangerous with
a public destructor....

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Christoph Kliemt
Guest





PostPosted: Mon Dec 05, 2005 12:29 pm    Post subject: Re: singleton classes. Reply with quote



"krish" <krishnakanna (AT) rediffmail (DOT) com> writes:

Quote:
I have a problem with the singleton classes... I have a main
controller class like the following

[...]

Quote:
My question is instead of declaring it local to every function and
using it, is there anyway that i can create a reference of this object
globally somewhere inside the class declaration and use it across all
my functions??? it wud be great if anyone throws some light on this
area..

Hmm... define it in say "main" compilation unit, and use it via "extern" ?

hth,

Christoph


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Back to top
Zhenghui Zhou
Guest





PostPosted: Wed Dec 07, 2005 2:27 pm    Post subject: Re: singleton classes. Reply with quote

Singleton is a some "strange" idiom in modern program languages, and
lead to the main field dealed with something called IOC container in
"pure" OO languages, like Java/C#, depending on their dynamic
functionality.

In procedural language, like C, people never mention of it, since they
use to define global/extern variables as well as global/in-file-visible
variables which relative to the advantage which OO brings.

So I just give some individual opinions here. Comparing IOC container
with global/extern variables, the "pure" OO system puts the definition
and creation of objects to an additional configuration step instead of
mixing it up the consumption. The dynamic abilities, e.g. virutal
interface and dynamic creativity of objects, show us what a "fashion"
language can offer.

But the key is the dependence between objects, come into being the
human's mind before. In type structural design, interface would be
defined first and the relationship between classes is built statically,
if one can bewrite the hierarchy clearly, or should refactor it. A
propriety OO design works well, in particular it was mapped from the
true world.

Also the couple may be loosen as the type information cannot be
determined easily or just we would not like. In high order program, one
needn't to structure the type hierarchy very well as he begin to work,
but to know the datas need in the functions he just wrote, in common,
simple type datas in strong type languages. The singleton or any other
dependence are delay to when need indeed. Then we may define the
instance in some compilation unit, mostly, the "main" compilation as
someone mentioned above, and may be treated like the configuration part
of the program. Each program need a configuration part first I think.

The codes may like:

class mainctrl
{
public:
void initialize(subctrl data);

};

class subctrl
{
// ... just some other things.
};

class someprocedure
{
public:
boost::signal<void()> singal_initialize;
void initializefunction(); // may need to initialize the mainctrl, the
singal above will be throw here.
};

In "main" compilation, using the global/extern definition in the
beginning:

extern subctrl subinstance;

int main()
{

mainctrl maininstance;
someprocedure procedureinstance;

// you may initialize the mainctrl directly or connect to the
inistialize signal;

procedureinstance.signal_initialize.connect(
boost::bind(&mainctrl::initialize
, &maininstance
, boost::ref(subinstance)
)
);

// ...
}


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.