bool Box::operator==(const Shape& s) const {
return hght == dynamic_cast(s).hght &&
wdth == dynamic_cast(s).wdth &&
dpth == dynamic_cast(s).dpth;
}
Answer:
The cast of the reference to a Shape to a reference to a Box is necessary to enable access to the size instance variables of the Box object. We call this a dynamic cast. Without this cast, the compiler would generate an error to the effect that the size variables are not members of the Shape class:
bool Box::operator==(const Shape& s) const {bool Box::operator==(const Box& s) const { // ERROR does not define
return hght == s.hght && // ERROR hght is not a member of s
wdth == s.wdth && // ERROR wdth is not a member of s
dpth == s.dpth; // ERROR dpth is not a member of s
}
On the other hand, receiving a reference to a Box object directly would accept the definition
// == const Shape&
return hght == s.hght && wdth == s.wdth && dpth == s.dpth;
}
No comments:
Post a Comment