如何使用shared_ptr并从enable_shared_from_this继承来创建克隆方法 [英] How To Make a clone method using shared_ptr and inheriting from enable_shared_from_this

查看:60
本文介绍了如何使用shared_ptr并从enable_shared_from_this继承来创建克隆方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现编写返回boost :: shared_ptr的克隆方法的一种有用方法是

I have seen that a useful way to write a clone method that returns a boost::shared_ptr is to do

class A
{
public:
  shared_ptr<A> Clone() const
  {
    return(shared_ptr<A>(CloneImpl()));
  }
protected:
  virtual A* CloneImpl() const
  {
    return(new A(*this));
  }
};

class B : public A
{
public:
  shared_ptr<B> Clone() const
  {
    return(shared_ptr<B>(CloneImpl()));
  }
protected:
  virtual B* CloneImpl() const
  {
    return(new B(*this));
  }
};

这允许与常规指针一起使用协方差,同时仍将其包装在智能指针的安全范围内.我的问题是我的类B需要从boost :: enable_shared_from_this继承,因为在构造之后,它需要向一个单独的类注册自己,并将共享的指针传递给它自己.我有一个Create方法,用于包装构造和注册以确保它们始终在一起.但是,上述克隆方法实现无法满足此要求.在CloneImpl中无法进行注册,因为尚不存在拥有"该对象的shared_ptr,从而阻止了对shared_from_this()的调用,并且如果此逻辑不在虚拟函数中,则指向B的shared_ptr不会知道B的注册需求克隆时.解决此问题的最佳方法是什么?

This allows the use of covariance with the regular pointer while still wrapping it in the safety of a smart pointer. My problem is my class B needs to inherit from boost::enable_shared_from_this because right after construction it needs to register itself with a separate class, passing a shared pointer to itself. I have a Create method that wraps construction and registration to make sure these always occur together. The above clone method implementation cannot handle this requirement, however. The registration cannot occur in CloneImpl since no shared_ptr yet exists "owning" the object, preventing a call to shared_from_this(), and if this logic is not in the virtual function then an shared_ptr that points to B, does not know about B's registration needs when cloned. What is the best way to handle this problem?

推荐答案

由于您已经通过非虚拟 Clone()函数自己实现了 public 接口协方差,您可以考虑放弃 CloneImpl()函数的协方差.

Since you are already implementing the public interface covariance yourself via the non-virtual Clone() functions, you may consider abandoning the covariance for the CloneImpl() functions.

如果您只需要shared_ptr而不需要原始指针,那么您可以这样做:

If you only need shared_ptr and never the raw pointer, so you could then do:

class X
{
public:
  shared_ptr<X> Clone() const
  {
    return CloneImpl();
  }
private:
  virtual shared_ptr<X> CloneImpl() const
  {
    return(shared_ptr<X>(new X(*this)));
  }
};

class Y : public X
{
public:
  shared_ptr<Y> Clone() const
  {
    return(static_pointer_cast<Y, X>(CloneImpl())); // no need for dynamic_pointer_cast
  }
private:
  virtual shared_ptr<X> CloneImpl() const
  {
    return shared_ptr<Y>(new Y(*this));
  }
};

CloneImpl()将始终返回 shared_ptr< Base> ,现在您可以在 B :: CloneImpl()中注册对象.函数并返回已注册的shared_ptr.

CloneImpl() would always return a shared_ptr<Base> and now you could register your object inside the B::CloneImpl() function and return the registerd shared_ptr.

这篇关于如何使用shared_ptr并从enable_shared_from_this继承来创建克隆方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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