locked
how to cast a unique_ptr from base class to derived class? RRS feed

  • Question

  • How to cast a unique_ptr to a base class => unique_ptr to a derived class?

    I use dynamic_pointer_cast to do this casting with a shared_ptr.
      shared_ptr<BaseClass> p1( new DerivedClass( args )) ;
      shared_ptr<DerivedClass> p2 = dynamic_pointer_cast<DerivedClass>(p1) ;

    but the compiler says no when the pointer is a unique_ptr.

    I am reading that I should use get( ) to get the raw pointer, then release the unique_ptr, then create the unique_ptr to the raw pointer to the derived class. Is that the way to do it?

    thanks,

    Tuesday, May 29, 2012 5:39 PM

Answers

  • That seems to be because the compiler is being unable to cast the pointer p1 appropriately.  if you use p1.get() instead of just p1 it should work just fine.  I quickly tried it in VS2010 and it seems to work.

    Jose R. MCP

    • Marked as answer by Steve Richter Tuesday, May 29, 2012 8:12 PM
    Tuesday, May 29, 2012 7:09 PM
  • On 5/29/2012 1:39 PM, Steve Richter wrote:

    How to cast a unique_ptr to a base class =>  unique_ptr to a derived class?

    unique_ptr<BaseClass> pBase = ...;  // initialized somehow to hold DerviedClass*
    unique_ptr<DerivedClass> pDerived(static_cast<DerivedClass*>(pBase.release()));

    Be careful not to end up with two unique_ptr's both thinking they own the same object.


    Igor Tandetnik

    • Marked as answer by Steve Richter Wednesday, May 30, 2012 3:08 PM
    Tuesday, May 29, 2012 9:51 PM

All replies

  • And can't you convert it simply by constructing from a converted pointer?

    unique_ptr p2(dynamic_cast<DerivedClass*>(p1));

    I assume p1 will provide a cast operator to BaseClass*, which will be used by dynamic_cast<> and all should be good.  Try it out.  I am guessing it works.


    Jose R. MCP

    Tuesday, May 29, 2012 5:59 PM
  • And can't you convert it simply by constructing from a converted pointer?

    unique_ptr p2(dynamic_cast<DerivedClass*>(p1));

    I assume p1 will provide a cast operator to BaseClass*, which will be used by dynamic_cast<> and all should be good.  Try it out.  I am guessing it works.


    Jose R. MCP

    does not work. I get a error: the operand of a pointer dynamic_cast must be a pointer to a complete class type.

        unique_ptr<RegPartBase> p1(new ValueNamePart(L"abc")) ;
        unique_ptr<ValueNamePart> p2( dynamic_cast<ValueNamePart*>(p1)) ;
    
    // RegPartBase is the base class.
    // ValueNamePart is a class derived from RegPartBase.
    

    There would have to be some sort of a move_with_cast function to move the pointer and cast it at the same time.

    Tuesday, May 29, 2012 6:25 PM
  • That seems to be because the compiler is being unable to cast the pointer p1 appropriately.  if you use p1.get() instead of just p1 it should work just fine.  I quickly tried it in VS2010 and it seems to work.

    Jose R. MCP

    • Marked as answer by Steve Richter Tuesday, May 29, 2012 8:12 PM
    Tuesday, May 29, 2012 7:09 PM
  • On 5/29/2012 1:39 PM, Steve Richter wrote:

    How to cast a unique_ptr to a base class =>  unique_ptr to a derived class?

    unique_ptr<BaseClass> pBase = ...;  // initialized somehow to hold DerviedClass*
    unique_ptr<DerivedClass> pDerived(static_cast<DerivedClass*>(pBase.release()));

    Be careful not to end up with two unique_ptr's both thinking they own the same object.


    Igor Tandetnik

    • Marked as answer by Steve Richter Wednesday, May 30, 2012 3:08 PM
    Tuesday, May 29, 2012 9:51 PM