Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7911671
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:20:06+00:00 2026-06-03T13:20:06+00:00

I was going through the example of RTTI here on wikipedia . I am

  • 0

I was going through the example of RTTI here on wikipedia.

I am confused about this part

abc *abc_pointer = new xyz();

If abc_pointer is being made to point to a xyz object wouldnt it be obvious that that it will get identified.I mean what is the use of comparing

xyz_pointer != NULL

later on and RTTI in general then? Am I missing something here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T13:20:09+00:00Added an answer on June 3, 2026 at 1:20 pm

    The important bit is when they do:

    xyz_pointer = dynamic_cast<xyz*>(abc_pointer);
    

    Later on to cast it back to an xyz. Not all abcs will be xyzs, even though all xyzs are abcs. Here the dynamic_cast says “if it is one of these then cast it, otherwise stop and give me NULL instead of doing bad things”. dynamic_cast is using RTTI for you.

    In the toy example you can clearly tell (and the compiler could even figure it out if it wanted to) that the abc* was an xyz* too. Imagine the function:

    void (abc *ptr) {
      if (dynamic_cast<xyz*>(ptr)) {
        //...
      }
      else {
        //...
      }
    }
    

    In that general instance there’s no way to tell if what you’re given can be cast to an xyz without looking at its type information at runtime, which is exactly what dynamic_cast does.

    Note that had you used static_cast<xyz*>(ptr) the cast would always look to have worked, even in cases where it’s not actually legal to do so and most likely lead to undefined behaviour.

    It should be noted here though that if you find yourself writing lots of if (dynamic_cast<... that might indicate a “code smell” – you should consider refactoring, probably a virtual method would be more appropriate. dynamic_cast and RTTI should be a last resort when designing C++.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm learning javascript and was going through an example here: https://developer.mozilla.org/en/A_re-introduction_to_JavaScript function personFullName() {
So say for example I'm going through an 'if' block and in this block,
After going through a lot of articles on IDisposable , I got confused about
Hi I'm going through regular expressions but I'm confused about metacharacters, particularly '*' and
I was going through operator precedence section of php.net and came across this example
I'm new to C# and going through an example in a book. The example
I was going through a tutorial about defining functions with an example on creating
I am relatively new to Java. I came across this statement while going through
I am going through a tutorial, and it showed this example code to make
I am going through some example assembly code for 16-bit real mode. I've come

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.