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 

The right way to implement a common datasource for a hieracy

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Christofer Fransson
Guest





PostPosted: Mon Oct 25, 2004 3:24 pm    Post subject: The right way to implement a common datasource for a hieracy Reply with 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
Back to top
Chris Whitworth
Guest





PostPosted: Mon Oct 25, 2004 3:29 pm    Post subject: Re: The right way to implement a common datasource for a hie Reply with quote



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





PostPosted: Mon Oct 25, 2004 3:34 pm    Post subject: Re: The right way to implement a common datasource for a hie Reply with quote




"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





PostPosted: Mon Oct 25, 2004 3:48 pm    Post subject: Re: The right way to implement a common datasource for a hie Reply with quote

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





PostPosted: Tue Nov 16, 2004 8:57 am    Post subject: Re: The right way to implement a common datasource for a hie Reply with quote

I would like to express my gratitude for the suggested solutions.

/Christofer
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) 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.