typeid
keyword
Introduced in CS247, when introducing the Liskov Substitution Principle.
bool Circle::operator==(const Shape& other) const {
if (typeid(other) != typeid(Circle)) return false;
const Circle& cother=static_cast<const Circle&>(other);
...
}
typeidreturns
std::type_info` objects that can be compared.
Difference between
typeid
anddynamic_cast
?
typeid
: Isother
exactly aCircle
? Doesn’t consider inheritance.dynamic_cast
: Isother
aCircle
, or a subclass ofCircle
? Considers inheritance.
typeid uses the dynamic-type so long as the class has at least one virtual method.