拥有std :: shared_ptr和std :: make_shared

时间:2014-06-20 08:16:39

标签: templates c++11 shared-ptr

对于调试情况,我需要实现自己版本的shared_ptr类。典型的当我使用std :: shared_ptr时,为方便起见我使用了typedef:

typedef std::shared_ptr<myclass> myclassptr;

在调试情况下我想扩展shared_ptr模板,而不是类myclass,使用一些额外的调试方法而不是使用typedef:

class myclassptr : public std::shared_ptr<myclass>
{
  public:
   // some special tracking methodes
};

但是我留下了一个演员,但是没有编译:

myclassptr mcp=std::make_shared<myclass>();

我已经将make_shared封装在像工厂这样的工厂中:

myclassptr createMyClass()
{
  return std::make_shared<myclass>();
}

但我怎样才能进入我的调试版本?

1 个答案:

答案 0 :(得分:1)

myclassptr一个接受std::shared_ptr的构造函数(也可能是赋值运算符):

class myclassptr : public std::shared_ptr<myclass>
{
  public:
   // some special tracking methodes

    myclassptr(std::shared_ptr<myclass> arg)
      : std::shared_ptr<myclass>(std::move(arg))
    {}

    myclassptr& operator= (std::shared_ptr<myclass> src)
    {
      std::shared_ptr<myclass>::operator=(std::move(src));
      return *this;
    }
};

根据您使用myclassptr的方式,您可能希望也可能不希望将构造函数标记为explicit