How to perform a dynamic_cast with a unique_ptr?

后端 未结 4 885
忘了有多久
忘了有多久 2020-12-15 16:31

I have a class hierarchy as follows:

class BaseSession : public boost::enable_shared_from_this
class DerivedSessionA : public BaseSession
         


        
4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-15 17:32

    Unless you want to transfer ownership of your std::unique_ptr, your function should take pointer or reference to T.

    So signature of Func should be something like Func(DerivedSessionA*)

    and then your call may look like:

    std::unique_ptr ptr; // Initialize it with correct value
    
    Func(dynamic_cast(ptr.get()));
    

    Or as you seems to call it directly from a method in BaseSession:

    Func(dynamic_cast(this));
    

提交回复
热议问题