c++ - How to do perform a dynamic_cast with a unique_ptr? -
i have class hierarchy follows:
class basesession : public boost::enable_shared_from_this<basesession> class derivedsessiona : public basesession class derivedsessionb : public basesession within derived class functions, regularly phone call functions this:
func(boost::dynamic_pointer_cast<derivedsessiona>(shared_from_this())); since working shared_ptr manage sessions, working fine. recently, discovered utilize of shared_ptr not optimal case. because these sessions singleton objects maintain 1 socket per client. if socket reconnected, session copies used become zombies.
as workaround, started passing shared_ptr reference rather copies. solved zombie problem.
ideally, felt should using unique_ptr store session , pass references other functions. opened whole can of worms.
how cast base of operations class unique_ptr object derived class unique_ptr object? unique_ptr version of next line?
func(boost::dynamic_pointer_cast<derivedsessiona>(shared_from_this())); i want 1 re-create of session object, else should reference.
unless want transfer ownership of std::unique_ptr<t>, function should take pointer or reference t.
so signature of func should func(derivedsessiona*)
and phone call may like:
std::unique_ptr<basesession> ptr; // initialize right value func(dynamic_cast<derivedsessiona*>(ptr.get())); or seems phone call straight method in basesession:
func(dynamic_cast<derivedsessiona*>(this)); c++ boost smart-pointers unique-ptr dynamic-cast
No comments:
Post a Comment