I am trying to get a new derived class pointer (with new memory location) from a parent class pointer that points to a derived class.
#include <iostream>
#include <random>
class parent{
public:
virtual void print(){
printf("p\n");
}
};
class dev1: public parent{
public:
void print(){
printf("d1\n");
}
};
class dev2:public parent{
public:
void print(){
printf("d2\n");
}
};
int main(int argc, const char * argv[]) {
srand(time(NULL));
parent*bob;
if(rand()%2){
bob = new dev1();
}else{
bob = new dev2();
}
bob->print();
parent * bobwannabe;
bobwannabe = new typeof(*bob);
bobwannabe->print();
return 0;
}
I was hoping bobwannabe would print what bob prints. bobwannabe shouldn't point to the same object as bob but should be the same class as bob.
Right now, bobwannabe becomes a new parent. Is there a way to turn it into a new bob with it's current type or is there a specific term I should be searching up? Thank you!