c++ - is this code safe? cast a std::vector to std::vector -
c++ - is this code safe? cast a std::vector<Derived*> to std::vector<Base*> -
basically i've class a
, class b : public a
.
and i'd cast std::shared_ptr<std::vector<a*>
std::shared_ptr<std::vector<b*>
the problem std::vector<b>
doesn't inherit std::vector<a>
, , smart_ptr
neither. horrible cast:
std::shared_ptr<vectora> vector_a = * ((std::shared_ptr<vectora>*)&vector_b);
the code compiles , runs there, safe? http://liveworkspace.org/code/3dqtz1$0
strictly speaking dereferencing operations should succeed. both vectors pointer containers, casting 1 another, whilst unacceptable production code, still utilize same dimensions , alignment.
c++ provides rich abstractions avoid these shenanigans though, improve populate vector of derived objects pointers base of operations class.
c++ inheritance casting
Comments
Post a Comment