tags:

views:

99

answers:

3

Hi!

I was wondering what is the best practice re. passing (another class) amongst two instances of the same class (lets call this 'Primary'). So, essentially in the constructor for the first, i can initialize the outside instance (lets call this 'Shared') - and then set it to a particular value whilst im processing this class in main().

So 'Shared', may be an int, say 999 by now.

Now what if i create another instance of the main class 'Primary'? whats the best way to access the already initialized outside instance of 'Shared' - because if i don't handle this correctly, the constructor for 'Primary', when called again will just go ahead and create one more instance of 'Shared', and thus i loose the value 999.. i can think of some messy solutions involving dynamic pointers and if statements (just) but i have a feeling there might be a simpler, cleaner solution?

+2  A: 

Make the constructor take a pointer or reference to the shared class. It is easier to construct outside.

class Shared;

class Same
{
    shared& shared_;

    Same( Shared& s ) { shared_ = s; }
}

With appropiate use of const and other constructors etc.

Mark
One thing that needs to be worked out if you're going to have references to a shared instance of a class is ownership - who is responsible for cleaning up the `Shared` object instance? Is it one of the `Same` instances or something else altogether? There's no one right answer - but it's something that needs to be considered in how the sharing is designed.
Michael Burr
Agreed re ownership.
Mark
+3  A: 

As I understand it:

  • You have a class A
  • You have a class B
  • For all members of class A there is a single instance of class B

You did not mention if any parameters from the A constructor are used to initialize B!
What happens to the parameters of the second A that are used for B?
So we will assume that B is default constructed.
We will also assume that you need the instance of B to be lazily evaluated otherwise you would just use a static member.

class A
{
    B&   bRef;
    public:
        A()
          :bRef(getLazyB())     // Get a reference to the only B for your object.
        {}                      

    private:
        static B& getLazyB()
        {
            static B instance; // Created on first use
            return instance;   // returned to all users.
        }
};
Martin York
thanks man! awesome :)
oneAday
However this introduces a singleton that will have issues with threading and testing. Also you cant send parameters to its constructor. It depends on the details of what you need
Mark
@Mark: Yes this absolute simplistic demo introduces a singelton. But ina tdd environment it is simpicity itself to turn getLazyB() into a factory to generate the appropraite object. But I find it is usually better to answer one question at a time. Not try and explain the whole of C++ in one fell swoop.
Martin York
@Martin - No need to use one fell swoop; feel free to use 2 or 3 comment slots to explain the whole of C++.
Michael Burr
C -> C++ -> RAW Pointers -> Smart Pointers -> RAII (Done)
Martin York
A: 

This depends on the semantics of your classes. If the outside class is not really outside but some obscure implementation detail that happens to be shared between instances, pass the first instance to the constructor of the second instance and get a reference to the outside instance there.

If the outside class is really an outside class with a meaning by itself, create it outside and pass it to the constructor just as Mark suggested.

If not only two specific instances but all instances share the same instance of the outside class, think about making it a static member of the class, as Martin York suggested.

Malte Clasen