OO Design question: How to pass around very common classes

S

Søren Johansen

Hi,

I am making an application that is beginning to turn quite large.
This application has some classes that are known by almost every other class
namely CApplication, CProject and the like.
So far, I have been passing around references to these classes in the
constructors of all the other classes but it is beginning to annoy me to
have to do this.
I could easily make global variables, it wouldn't be unsafe in the sense
that I know that the instances are always present and that there is only
one. Still, this solution not pleasing to the eye :) and I gather there
might turn up some thread safety issues or the like (currently, everything
runs in a single thread) in the future.

Any alternative suggestions would be very welcome.

Søren
 
J

John Dibling

Hi,

I am making an application that is beginning to turn quite large.
This application has some classes that are known by almost every other class
namely CApplication, CProject and the like.
So far, I have been passing around references to these classes in the
constructors of all the other classes but it is beginning to annoy me to
have to do this.
I could easily make global variables, it wouldn't be unsafe in the sense
that I know that the instances are always present and that there is only
one. Still, this solution not pleasing to the eye :) and I gather there
might turn up some thread safety issues or the like (currently, everything
runs in a single thread) in the future.

Any alternative suggestions would be very welcome.

Søren

You could make a static member function in the class you need to 'pass
around', like this:

class Application
{
public:
static Application* GetApplicationInstance() { return
s_mpSingleton; }
protected:
static Application* s_mpSingleton;
}; // Application ///

Note that you will need to add code which initializes the pointer,
ensures the object is in fact a singlerton, etc.

</dib>

John Dibling
email: dib@substitute_my_full_last_name_here.com
Witty banter omitted for your protection
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top