The right way to implement a common datasource for a hieracy of classes?

  • Thread starter Christofer Fransson
  • Start date
C

Christofer Fransson

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
 
C

Chris Whitworth

100% (e-mail address removed) (Christofer Fransson)!
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
 
J

John Harrison

Christofer Fransson said:
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
 
R

Richard Herring

Christofer said:
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.)
 
C

Christofer Fransson

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

/Christofer
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,184
Messages
2,570,973
Members
47,527
Latest member
RoxanneTos

Latest Threads

Top