Lets say I have the class
template
class Parent {
public:
typedef boost::shared_ptr > Ptr;
inline Ptr
CRTP will work here:
template<typename Child>
class Parent {
public:
typedef boost::shared_ptr<Child> Ptr;
inline Ptr
makeShared ()
{
return Ptr (new Child(*static_cast<Child *>(this)));
}
};
template<typename PointT>
class Child : public Parent<Child> {
};
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
.