typeid()
only actually performs an RTTI lookup when passed an lvalue of a polymorphic type. Shape
is a polymorphic type, but you aren't passing a Shape
lvalue, you're passing a Shape*
. So when you are passing c
, s
and r
to typeid()
, it reports the static type of those expressions, which is Shape*
.
To get a run time lookup you can either dereference your pointer: std::cout << typeid(*r).name() << std::endl;
or you can keep references directly:
Circle circle{4};
Shape& c = circle;
cout << "Area of circle:" << c.area() << endl;
cout << typeid(c).name() << endl;