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

  • Home
  • SEARCH
  • 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 7635443
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T07:25:38+00:00 2026-05-31T07:25:38+00:00

Possible Duplicate: C++ equivalent of instanceof I was wondering what the difference between dynamic_cast

  • 0

Possible Duplicate:
C++ equivalent of instanceof

I was wondering what the difference between dynamic_cast and typeid is in regards to just class comparison (aside from dynamic_cast allowing access to the subclass’s methods and typeid only being useful for class comparison). I found a two year old StackOverflow asking the same question:
C++ equivalent of java's instanceof. However, it is two years old and I did not want to necro an old post (and I am unsure when typeid came out), so I thought to re-ask the same question with a slight difference.

Basically, I have class A and class B, which are both subclasses of abstract class C. Class C is being taken in as a parameter to a method and I want to determine if class C is really class A or class B. Both typeid and dynamic_cast work properly so this is more of a question of best practice/performance. I am guessing :

A* test = dynamic_cast<A*> someClassCVar
if (test != 0) { //it is of class A }

OR

if (typeid(someClassCVar) == typeid(A)) {
   //it is of class A
}

EDIT: Sorry, I forgot to include this bit of information. The ActiveMQ CMS documentation states to use dynamic_cast, but I think that is only because it assumes the user will want to access methods specific to the subclass. To me, it seems that typeid would be better performance if only a class comparison is needed:
https://activemq.apache.org/components/cms/overview

  • 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-05-31T07:25:39+00:00Added an answer on May 31, 2026 at 7:25 am

    There is an important difference between the two methods:

    if(A* test = dynamic_cast<A*>(&someClassCVar)) {
        // someClassCVar is A or publicly derived from A
    }
    

    Whereas:

    if(typeid(someClassCVar) == typeid(A)) {
       // someClassCVar is of class A, not a derived class
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: What is the difference between String.Empty and “” Is equivalent to String.Empty
Possible Duplicate: What is the difference between "typename" and "class" template parameters? When defining
Possible Duplicate: Equivalent of SQL ISNULL in LINQ? I am recently migrated from ADO.Net
Possible Duplicate: Static class variables in Python What is the Python equivalent of static
Possible Duplicate: Scala equivalent of Java java.lang.Class<T> Object Hi all, I can not call
Possible Duplicate: LINQ equivalent of foreach for IEnumerable<T> I'm wondering whether there is a
Possible Duplicate: C# eval equivalent? Hi - I was wondering how I can execute
Possible Duplicate: jQuery equivalent of JavaScript's addEventListener method Also from a very good jQuery
Possible Duplicate: Is there a call_user_func() equivalent to create a new class instance? I
Possible Duplicate: Qt equivalent of PathAppend? Is there a class that handles file paths

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.