Lets say I have the class
template
class Parent {
public:
typedef boost::shared_ptr > Ptr;
inline Ptr
CRTP will work here:
template
class Parent {
public:
typedef boost::shared_ptr Ptr;
inline Ptr
makeShared ()
{
return Ptr (new Child(*static_cast(this)));
}
};
template
class Child : public Parent {
};
Note that makeShared
is a fairly confusing name as it could be confused with shared_from_this (in C++11 and Boost). A more typical name for your method is clone
.