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 833597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:32:34+00:00 2026-05-15T04:32:34+00:00

My question is related to RTTI in C++ where I’m trying to check if

  • 0

My question is related to RTTI in C++ where I’m trying to check if an object belongs to the type hierarchy of another object. The BelongsTo() method checks this.
I tried using typeid, but it throws an error and I’m not sure about any other way how I can find the target type to convert to at runtime.

#include <iostream>
#include <typeinfo>

class X
{
    public:
        //  Checks if the input type belongs to the type heirarchy of input object type
        bool BelongsTo(X* p_a)
        {
            //  I'm trying to check if the current (this) type belongs to the same type 
            //  hierarchy as the input type
            return dynamic_cast<typeid(*p_a)*>(this) != NULL;   //  error C2059: syntax error 'typeid'
        }
};

class A : public X
{
};

class B : public A
{
};

class C : public A
{
};

int main()
{
    X* a = new A();
    X* b = new B();
    X* c = new C();
    bool test1 = b->BelongsTo(a);   // should return true
    bool test2 = b->BelongsTo(c);   // should return false
    bool test3 = c->BelongsTo(a);   // should return true
}

Making the method virtual and letting derived classes do it seems like a bad idea as I have a lot of classes in the same type hierarchy.
Or does anybody know of any other/better way to the do the same thing? Please suggest.

Update: b.BelongsTo(a) should detect if the input object type (a) is an ancestor of the current object (b) in the type hierarchy.

  • 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-15T04:32:35+00:00Added an answer on May 15, 2026 at 4:32 am

    This doesn’t make sense – the very fact that you can call the function means that the parameter belongs to the X hierarchy, as that is the type of the parameter. Dynamic casts are intended to find out the actual type within a known hierarchy.

    The syntax error in your code:

    return dynamic_cast<typeid(*p_a)*>(this) != NULL;  
    

    is because a typeid is not a type – you simply cannot use it as a type with dynamic_cast like that.

    If as Naveen suggests you want to find out if an instance belongs to a sub-hierarchy, use:

    if ( dynamic_cast <A*>( some_x_ptr ) ) {
    
        // yes, belongs to A sub-hierarchy
    }
    

    Edit: You have:

    A <- P <- X
    A <- Q <- Y
    

    Then:

    A * a = new X;
    
    dynamic_cast <P *>( a );   // not null
    dynamic_cast <Q *>( a );   // null
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Another question related to this one . I have a List<SortableObjects> that is the
I have a question related to this one : I'm trying to attach an
This is another question related to a question I asked a few minutes ago.
Simmilar question (related with Python2: Python: check if method is static ) Lets concider
I faced this puzzle question[ related to data structure ] in a coding competition.
I have a question related to this one . I don't want to do
i got a question related to the setMultiFile method of zend_form. I already got
This is a question related to Capture and log the response body . What
This is a question related to how people are handling a situation in the
I have a question related to jmock library. I am trying to mock a

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.