Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cast an entire vector

Is it possible to cast a std::vector<std::shared_ptr<Object>> to an std::vector<std::shared_ptr<SpecializedObject>> where SpecializedObject inherits Object, without building a new array (or iterate through the vector)?

like image 232
Talesseed Avatar asked Mar 20 '17 16:03

Talesseed


1 Answers

Short answer: no.

Long answer:

std::vector<std::shared_ptr<Object>> and std::vector<std::shared_ptr<SpecializedObject>> are completely different and unrelated beasts and you cannot cast from one type to the other one.
You must iterate through the vector and create a new one from that.

Hint: you can still use static_pointer_cast to cast the pointers while iterating (if you know what you are doing, of course).

like image 143
skypjack Avatar answered Sep 21 '22 09:09

skypjack