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?
Looks like you just need to pass your original Base class into your DerivedClass constructor:
class DerivedClass {
public:
DerivedClass(shared_ptr<Base> createFromBase) {
// do the copying from base
// and additional things this Derived needs
}
}
Then (with new C++ features):
auto testDerived = make_shared<DerivedClass>(test[0]);
or (same thing):
shared_ptr<DerivedClass> testDerived(new DerivedClass(test[0]));
If you want to manage pointers yourself (we call them sometimes naked pointers):
DerivedClass * testDerived = new DerivedClass(test[0]);
// in this case you have to make sure to call
delete testDerived;
// exactly once if you want to free memory
You can also create derived class as a variable (this is allocaled on stack):
DerivedClass testDerived(test[0]);
But you must have in mind that variables are destroyed automatically as soon as they reach end of scope:
{
DerivedClass testDerived(test[0]); // created
// use it
} // <-- destroyed automatically
That's why you don't return pointers to local variables from functions:
DerivedClass * fun() {
DerivedClass testDerived(test[0]); // created
// use it just fine here
return &testDerived; // <-- will return pointer to deleted variable
} // <-- destroyed automatically
However you can return pointer to your object if you allocate it on heap (with "new"):
DerivedClass * fun() {
DerivedClass * testDerived = new DerivedClass(test[0]); // created
return testDerived; // <-- will return pointer to heap object
}
However, you have to manage deletion yourself.
You can, however, wrap your new object into shared_ptr. Shared pointer calls "delete" on your object automatically as soon as there is no scope that uses it.
shared_ptr<DerivedClass> fun() {
shared_ptr<DerivedClass> testDerived(new DerivedClass(test[0])); // created
// use it
return testDerived; // <-- will return copy of shared_ptr which manages pointer to your object
}
It is important not to mix shared pointers and naked pointers.
I hope I haven't got carried too far away with this post