With virtual copy constructors, a class Vehicle
has a copy()
virtual member function that all inherited classes like Car
will implement.
Later some other class can have any type of vehicle as a member variable:
struct Foo {
Vehicle *v;
Foo(const Vehicle &veh) {
v = veh.clone();
}
};
I don't see the point. Why not get rid of clone()
and do new
"in place" like this
struct Foo {
Vehicle *v;
Foo(Vehicle *veh) {
v = veh; //veh has no clone()
}
~Foo() {
delete v;
}
};
//elsewhere
Foo f(new Car());
What are the drawbacks (other than it only working for "in place" creation)? Now nobody has to implement clone and everything seems much easier.