For a debug situation I need to implement an own version of the shared_ptr class. Typical when I use std::shared_ptr I use a typedef for convenience:
typedef std::shared_ptr<myclass> myclassptr;
in a debug situation I want to extend the shared_ptr template, not the class myclass, with some additional debug methods instead of using the typedef:
class myclassptr : public std::shared_ptr<myclass>
{
public:
// some special tracking methodes
};
but that leave me with a cast at, which does not compile:
myclassptr mcp=std::make_shared<myclass>();
I already encapsulate the make_shared in a factory funktion like:
myclassptr createMyClass()
{
return std::make_shared<myclass>();
}
but how can I get to my debug version?
Give myclassptr
a constructor (and probably assignment operator too) which accepts a 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;
}
};
Depending on how you use myclassptr
, you may or may not want to mark the constructor as explicit
.