how to cast from intrusive_ptr<parent> to intrusive_ptr<child>?

Viewed 57

I have to get a child class pointer that is wrapped in boost's intrusive_ptr from intrusive_ptr<parent_class> returned by a class function.

class parent{...}
class child: public parent{...}


intrusive_ptr<parent>  GetElementPtr(ElementID Id)  {...};



/* in some other cpp file*/

ElementID XXX = ...;
intrusive_ptr<child> childElem = GetElementPtr(XXX); // cannot do so like this
  
1 Answers

Either boost::static_pointer_cast or boost::dynamic_pointer_cast, depending on whether you are sure there is a child being pointed to. That is, the same determination you'd use to pick between static_cast and dynamic_cast if you had a parent * and wanted a child *.

Related