简体   繁体   中英

Passing object of a template class to constructor of another class

I have a template class,

template< typename T >
class A
{
private:
    T *array;
public:
    A(int size)
    {
         //Initialises array with size 
    }
}

Now I need to have a class B which receives object of class A as constructor argument and assigns it to B local member of A reference. How do I do that? I have tried like,

class B
{
private:
  template<class T>
  A<T> *a;
public:
  template<class T>
  B(A<T>(int) ar){
     //assign ar to a
  }
}

Can somebody help me to solve this problem?

UPDATE Actually what I want to achieve here is, think that class A is a generic circular buffer, which can be initialised in one shot like A< int > int_buffer(20); and the same int_buffer to be used across different classes say B and C(Producer and Consumer). Is this the correct way to achieve my goal or any better approach you can suggest.

You'll just need to make B a template as well:

template <typename T>
class B {
    A<T>* a;
public:
    B(A<T>* ar) : a(ar) {}
};

This runs upon the problem of shared pointers. It's unclear to me as the reader if B<T> will depend upon an A<T>* that is externally owned, or if the ownership of A<T>* is also being transfered to B<T> . C++11 has provided you some great tools for sharing pointers, and I'd strongly recommend that you take advantage of them.

If you want to share A<T>* but it will be owned externally use a shared_ptr .
If you transferring ownership of A<T>* on construction, signify that by using a unique_ptr .

EDIT:

B<T> contains a member object A<T> . But it is held by pointer. Holding a member by pointer or reference indicates that the objects use is shared or ownership is external.

Without knowing your design it's hard to present a good design principle, so I'll give some optional situations here with the recommendation that you adhere to the one that matches your situation:

  1. If B<T> 's A<T> member is only used within the single B<T> object, that member needs to be created within B<T> 's constructor or emplace constructed
  2. If B<T> 's A<T> member is shared among all B<T> it should be a static member of the B class
  3. If A<T> will be owned by B<T> but may only be received by pointer use a unique_ptr<A<T>> rather than A<T>* as the member type
  4. If A<T> will be shared externally and will not be deleted as long as at least one reference to it is maintained use use shared_ptr<A<T>> rather than A<T>* as the member type
  5. If A<T> is owned externally and may or may not have been destroyed use weak_ptr<A<T>> rather than A<T>* as the member type

Your question is not clear. Do you want to accomplish this?

class B
{
private:
  A<int> *a;
public:
  B(A<int> ar){
     //assign ar to a
  }
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM