Hi
This may seems like a wierd question but is there a way to cast a base class upto a derived class (both is a pointer of the class ),
What I have is a derived class pointer that is inside a vector of baseclass pointers, my question is if I could get a derived class pointer from the vector without using
(c style casting)
dynamic_cast
static_cast
as I got the impression from people that those things are bad, if there are no standard way to do this, would it be better if I Included a "Copy return" function in my base class (and ovverirde it from the derived class) where I would have something like this pseudo code down here?
class Base
{
//Magical Code here and there
base* Copy(const BaseToCopy);
//Magical Code here and there
}
base* Base::Copy(const BaseToCopy)
{
Base newBase;
//Magical Copy code from BaseToCopy into the newBase
return &newBase
}
std::vector<std::shared_ptr<Base>> test;
DerivedClass* testDerived;
testDerived = test[0]->Copy(&test[0]); As this would theoretical work am not sure if this is a good way to do it or not, or if there exist a more so called "elegant" way to do it.
P.S
If I just should cast, which of the cast should I use if I know 100% for sure which class it is? or should always go safe with dynamic_cast?