将模板类的对象传递给另一个类的构造函数 [英] Passing object of a template class to constructor of another class

查看:136
本文介绍了将模板类的对象传递给另一个类的构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模板类,

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

现在,我需要一个类B,该类接收类A的对象作为构造函数参数并将其分配给A引用的B本地成员。我怎么做?我尝试过,

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?

更新
实际上,我想在这里实现的是,认为A类是通用的循环缓冲区,可以像 A<整数> int_buffer(20); 和用于不同类(例如B和C(生产者和消费者))的同一个int_buffer。这是实现我的目标的正确方法还是您可以建议的任何更好的方法。

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.

推荐答案

您只需要使 B 模板:

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

这是基于共享指针的问题。作为读者,我尚不清楚 B 是否取决于外部的 A * 所有权,或者如果 A< * 的所有权也正在转移到 B< T> 。 C ++ 11为您提供了一些共享指针的出色工具,我强烈建议您利用它们。

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.

如果要共享 A&T; * ,但它将使用 shared_ptr

如果您转让 A< T> * 在构造上,请使用 unique_ptr

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.

编辑:

B< T> 包含成员对象 A< T> 。但是它是由指针持有的。 通过指针或引用持有成员表示对象的使用是共享的,或者所有权是外部的。

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. 如果 B< T> A< T> 成员仅在单个 B< ; T> 对象,该成员需要在 B< T> 的构造函数或Emplace构建的内部创建

  2. 如果 all之间共享 B A< 成员 B< T> 它应该是 B 类的静态成员

  3. 如果 A< 将由 B< T> 拥有,但只能由指针使用来接收 unique_ptr< A< T>> c而不是 A< T> * 作为成员类型
  4. 如果 A&T; 将在外部共享并且不会被删除,只要至少保留了对其的一个引用,请使用 shared_ptr< A< T>> 而不是 A&T; * 作为成员类型

  5. 如果 A< T> 是外部拥有的,并且可能已被销毁或未被销毁,请使用 weak_ptr< A< T>> 而不是 A< T ** 作为成员类型

  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

这篇关于将模板类的对象传递给另一个类的构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆