| View previous topic :: View next topic |
| Author |
Message |
Christofer Fransson Guest
|
Posted: Mon Oct 25, 2004 3:24 pm Post subject: The right way to implement a common datasource for a hieracy |
|
|
Hi,
I have a huge datafile that I store in a std::map structure. I have
created a small class that handles parsing and memory allocation of
the datafile in a class called that we can call MyData.
I would like this MyData class to be known to an hierarcy of classes.
I do not want it as a baseclass because I only whish to have one
instance of it.
My solution right now is to submit a pointer to every constructor in
my class hiearacy that would like to get access from my class MyData.
I assume that this solution is an ugly implementation.
I dont like global variables.
What should I do? Put the MyData class as a static member at the top
of my class hierarcy?
Best regards,
Christofer
|
|
| Back to top |
|
 |
Chris Whitworth Guest
|
Posted: Mon Oct 25, 2004 3:29 pm Post subject: Re: The right way to implement a common datasource for a hie |
|
|
100% [email]christofer_fransson (AT) yahoo (DOT) com[/email] (Christofer Fransson)!
| Quote: | Hi,
I have a huge datafile that I store in a std::map structure. I have
created a small class that handles parsing and memory allocation of
the datafile in a class called that we can call MyData.
I would like this MyData class to be known to an hierarcy of classes.
I do not want it as a baseclass because I only whish to have one
instance of it.
My solution right now is to submit a pointer to every constructor in
my class hiearacy that would like to get access from my class MyData.
I assume that this solution is an ugly implementation.
I dont like global variables.
What should I do? Put the MyData class as a static member at the top
of my class hierarcy?
|
I think what you might be after is a Singleton.
http://www.codeproject.com/cpp/singletonrvs.asp
(although I'm not sure why he uses instanceFlag there, as you can just
check whether single is NULL or not in the getInstance() method)
Chris
--
Chris Whitworth | GT/AC Name: parm
http://home.parm.net | AC Town: Markham
|
|
| Back to top |
|
 |
John Harrison Guest
|
Posted: Mon Oct 25, 2004 3:34 pm Post subject: Re: The right way to implement a common datasource for a hie |
|
|
"Christofer Fransson" <christofer_fransson (AT) yahoo (DOT) com> wrote
| Quote: | Hi,
I have a huge datafile that I store in a std::map structure. I have
created a small class that handles parsing and memory allocation of
the datafile in a class called that we can call MyData.
I would like this MyData class to be known to an hierarcy of classes.
I do not want it as a baseclass because I only whish to have one
instance of it.
My solution right now is to submit a pointer to every constructor in
my class hiearacy that would like to get access from my class MyData.
I assume that this solution is an ugly implementation.
I dont like global variables.
What should I do? Put the MyData class as a static member at the top
of my class hierarcy?
Best regards,
Christofer
|
Singleton pattern? E.g.
class MyData
{
friend MyData& get_my_data();
private:
MyData();
public:
...
};
MyData& get_my_data()
{
static MyData the_one_and_only;
return the_one_and_only;
}
MyData constructors are private so only the friend function get_my_data can
create MyData objects (and it creates only one). All your other classes can
call get_my_data() when they need to.
Many other variations on this theme are possible.
John
|
|
| Back to top |
|
 |
Richard Herring Guest
|
Posted: Mon Oct 25, 2004 3:48 pm Post subject: Re: The right way to implement a common datasource for a hie |
|
|
In message <e128b049.0410250724.43c7a7c5 (AT) posting (DOT) google.com>, Christofer
Fransson <christofer_fransson (AT) yahoo (DOT) com> writes
| Quote: | Hi,
I have a huge datafile that I store in a std::map structure. I have
created a small class that handles parsing and memory allocation of
the datafile in a class called that we can call MyData.
I would like this MyData class to be known to an hierarcy of classes.
I do not want it as a baseclass because I only whish to have one
instance of it.
My solution right now is to submit a pointer to every constructor in
my class hiearacy that would like to get access from my class MyData.
I assume that this solution is an ugly implementation.
I dont like global variables.
What should I do? Put the MyData class as a static member at the top
of my class hierarcy?
A web search for "C++ singleton" should yield plenty of suggestions (and |
also discussion of how to deal with the potential pitfalls, which are
why I'm not offering any one-size-fits-all sample code right here.)
--
Richard Herring
|
|
| Back to top |
|
 |
Christofer Fransson Guest
|
Posted: Tue Nov 16, 2004 8:57 am Post subject: Re: The right way to implement a common datasource for a hie |
|
|
I would like to express my gratitude for the suggested solutions.
/Christofer
|
|
| Back to top |
|
 |
|