English 中文(简体)
Passing another class amongst instances
原标题:
  • 时间:2009-11-09 18:57:31
  •  标签:
  • c++
  • oop

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?

最佳回答

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.
        }
};
问题回答

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.

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.





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签