Question: C++ provides a typeid operator that can be used to query the concrete type of a pointer or reference variable: if (typeid(*p) == typeid(my_derived_type)) ...

C++ provides a typeid operator that can be used to query the concrete type of a pointer or reference variable:

if (typeid(*p) == typeid(my_derived_type)) ...

Values returned by typeid can be compared for equality but not assigned. They also support a name() method that returns an (implementation dependent) character string name for the type. Give an example of a program fragment in which these mechanisms might reasonably be used.

Unlike more extensive reflection mechanisms, typeid can be applied only to (instances of) classes with at least one virtual method. Give a plausible explanation for this restriction.

Step by Step Solution

3.46 Rating (162 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

ANSWER Example Check whether a pointer points to an obj... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Language Pragmatics Questions!