Thanks for contributing an answer to Stack Overflow! In other words, if you can make sure, by other means, that the pointer does indeed point to an instance of the derived class, then you can use static_pointer_cast, but otherwise, it is safer to use dynamic_pointer_cast. Is there any evidence suggesting or refuting that Russian officials knowingly lied that Russia was not going to attack Ukraine? How do I dynamic upcast and downcast with smart pointers? Can I takeoff as VFR from class G with 2sm vis. Why do some images depict the same constellations differently? Edit: Changing inheritence from Base to public Base made it work. But in this way I create another vector. No, I don't think so, in this forum, we like continuing discussions, and that's what this is. You could potentially also use dynamic_cast directly on the pointer managed by the smart pointer, if you don't want the ownership to be shared between the returned derived pointer: For the smart pointer itself it depends on the smart pointer. My code looks more like this (when posting the original question I forgot derived isn't concrete either): This last example is much more realistic to my actual code, is this not allowed? To learn more, see our tips on writing great answers. The earlier an error is discovered, the easier is it to identify the cause and rectify it. You can't cast shared_ptr<vector<Derived*>> to shared_ptr<vector<Base*>> because vector<Derived*> and vector<Base*> are two distinct types that are not related to each other.. That Derived and Base are related to each other does not matter in that case. Connect and share knowledge within a single location that is structured and easy to search. Cast from base class to a specific derived class when using a vector of shared pointers, Downcasting to base type of vector of shared_ptr. Is there a legal reason that organizations often refuse to comment on an issue citing "ongoing litigation"? Does Russia stamp passports of foreign tourists while entering or exiting Russia? Using enable_shared_from_this, you can send a shared_ptr from this to the function func, ensuring only shared_ptr to the object allocation space are used in your program. Efficiently match all values of a vector in another vector, Negative R2 on Simple Linear Regression (with intercept). Here is the member vector in manager class: CInputManager: // In class: CInputManager private: std::vector<std::tr1::shared_ptr<IInputDevice>> m_vecpInputDevices; I also have a few helper functions, that is meant to retrieve a specific input device (i.e: keyboard, mouse, etc). (since C++20) Going on memory here, try this (but note the cast will return NULL as you are casting from a base type to a derived type): DerivedType * m_derivedType = dynamic_cast<DerivedType*> (&m_baseType); If m_baseType was a pointer and actually pointed to a type of DerivedType, then the dynamic_cast should work. Let me explain my use-case a bit further: I am having a configuration XML file in which many objects are described. Also, sd should be of type SecondDerived. What do the characters on this CCTV lens mean? Is there any way I can do this? The compiler error in
How To Make A Lobby Bot Gift You 2022, Group Of The Year 2022 Vote, What Is Robot Operating System, Ncaa Redshirt Rules Volleyball, Pirates Cove Bar And Grill, Benefits Of Educational Attainment,