I want to first create an object of a parent class and according to some condition create child object of child class and put that into parent object. Now while after passing the object to some function that function need to get access to child class method. Please see the code for clarification.
class Parent {
virtual f(){
print('Parent');
}
}
class Child: public Parent{
virtual f(){
print('Child')
}
}
void do_something(Parent &obj){
obj.f(); // this will print Child
}
int main(){
Parent obj;
if(cond){
// This is my actual question
// Not sure how to create a child obj here and put it into parent obj
Child obj = dynamic_cast<Child>(obj);
}
do_something(obj) // pass the child obj
}