I have a function that takes a reference to a base object (used as an output parameter). That function must create a derived object and pass it back to the caller through that reference.
The caller calls that function by dereferencing a pointer to base (in this case a smart ptr), and passing it as the argument to the function.
The problem is that even though the function changes the underlying object, the caller still can't cast the pointer to the derived object:
#include <iostream>
#include <memory>
using namespace std;
struct Base {
virtual void id() { cout << "Base" << endl; }
};
struct Derived: Base {
void id() override { cout << "Derived" << endl; }
};
void get_derived(Base &b) {
b = Derived();
b.id(); //Still prints "Base"
}
int main() {
auto b = make_shared<Base>();
b->id(); //prints "Base"
get_derived(*b);
b->id(); //prints "Base" still
auto f = dynamic_pointer_cast<Derived>(b);
if(!f) cout << "cast failed" << endl; //this branch taken
else cout << "cast succeeded" << endl;
}
Why is it that after b = Derived()
, the pointer to id()
in the virtual table of b
still points to Base
's id()
function instead of Derived
's vtable? And how do I change get_derived()
to make the b
reference refer to a Derived
object?
Note: I can't change the signature of get_derived()