I have a vector<boost::shared_ptr<Base>> object. I’m inserting an object of type Derived where Derived inherits Base like this: container.push_back(boost::shared_ptr<Base>(new Derived()));
I now want to call a function which takes a reference to a member of Derived, in order to modify this last entry I just inserted. This way, I don’t end up creating a temporary instance of this Derived object. This is how I’m able to do it: func(static_cast<Derived*>(&*container.back())->member);
This looks really scary-ugly for me. But if I try to do func(static_cast<Derived>(*container.back()).member); instead, the compiler says I need to provide a ctor for Derived which takes the args (const Base&) in order to convert an instance of Base to an instance of Derived, which I don’t need to do here… I just want to refer to my shared_ptr pointer as though it were a Derived* because I know it is because I just newed it.
No casts. No temporaries of
Derived.