I want to know the runtime type of a base class pointer, I know you can use dynamic_cast. is there any better way?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
dynamic_cast will only confirm your guess, and even that is not perfect. If C inherits from B which inherits from A,
dynamic_cast<B*>((A*)&theC)will work.typeidwill give you the actual type, but in a way that’s not quite useful for anything. You can’t create new objects of that same type, for instance.So, the biq question that remains is what your real goal is. In proper OO design, you should never need to know about the unbounded set of types that can be derived from a base type.