OO Design question: How to pass around very common classes

Discussion in 'C++' started by Søren Johansen, Jul 24, 2003.

  1. 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
     
    Søren Johansen, Jul 24, 2003
    #1
    1. Advertising

  2. Søren Johansen

    John Dibling Guest

    On Thu, 24 Jul 2003 17:52:07 +0200, "Søren Johansen"
    <> wrote:

    >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
     
    John Dibling, Jul 24, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Raymond Arthur St. Marie II of III

    very Very VERY dumb Question About The new Set( ) 's

    Raymond Arthur St. Marie II of III, Jul 23, 2003, in forum: Python
    Replies:
    4
    Views:
    517
    Raymond Hettinger
    Jul 27, 2003
  2. shanx__=|;-

    very very very long integer

    shanx__=|;-, Oct 16, 2004, in forum: C Programming
    Replies:
    19
    Views:
    1,717
    Merrill & Michele
    Oct 19, 2004
  3. Abhishek Jha

    very very very long integer

    Abhishek Jha, Oct 16, 2004, in forum: C Programming
    Replies:
    4
    Views:
    466
    jacob navia
    Oct 17, 2004
  4. Peter

    Very very very basic question

    Peter, Feb 8, 2005, in forum: C Programming
    Replies:
    14
    Views:
    541
    Dave Thompson
    Feb 14, 2005
  5. olivier.melcher

    Help running a very very very simple code

    olivier.melcher, May 12, 2008, in forum: Java
    Replies:
    8
    Views:
    2,376
Loading...

Share This Page