in c++11 standard, if class B inherits from class A, then 'B is an A'. However, i am still confused regarding this notion: look at this code:
class Base {
public:
virtual ~Base() {}
virtual Base* clone() const = 0;
};
class Derived : public Base {
public:
virtual Base* clone() const {
return new Derived(*this);
}
//<more functions>
};
we returned a pointer to Base from Derived, but if using this approach in this code:
Derived* d1 = new Derived();
Derived* d2 = d1->clone();
what we did is assigning Base*
in Derived*
!!
The Problem:
How comes this code compiles?
Wouldn't this cause slicing to Derived new object?
What really been returned from Derived clone function?
Does clone being virtual in the first place has any significance on this?
Aucun commentaire:
Enregistrer un commentaire