find out the class type inside the class and its children

◇◆丶佛笑我妖孽 提交于 2019-12-02 10:58:41

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!