User-defined and non-trivial implicit implementation of copy constructor and assignment operator in C++ do not copy virtual table pointer from source object. In brief - copy does not change the type of an object.
Perhaps a better way to explain this concept is to ask a question:
class Vehicle {
public:
Vehicle(int wheels):wheels(wheels){}
virtual void ToString() {
std::cout << "A " << wheels << " wheels vehicle\n";
}
protected:
int wheels;
};
class Car : public Vehicle {
public:
Car(const char* model):Vehicle(4), model(model){}
void ToString() {
std::cout << model << "\n";
}
private:
const char* model;
};
Vehicle* pV = new Vehicle(2);
Vehicle* pC = new Car("VW Beetle");
// Note: Following assignment would not copy vptr from *pC to *pV.
*pV = *pC;
pV->ToString();
What do you think would be printed?