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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:36:25+00:00 2026-05-13T16:36:25+00:00

I have a base class which has two child classes derived from it. class

  • 0

I have a base class which has two child classes derived from it.

class A {};
class B : public A {};
class C : public A {};

I have another class that has a pointer to collection of class A members using a vector, something like this:

vector<A*> *m_collection;

And what I do is to create objects of class B or C and add them to the collection using push_back:

B *myb = new B();
m_collection->push_back(myb);

Then I loop through the collection and try to check using ‘typeid’, but it always returns the base class (A). Is it not possible to know the exact type?

Thank you!

  • 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-13T16:36:26+00:00Added an answer on May 13, 2026 at 4:36 pm

    Firstly, there is unlikely to be a reason to create your vector dynamically using new. Simply say:

    vector<A*> m_collection;
    

    Then you need to give your base class a virtual function or two. A virtual destructor would be a good start:

    class A {
       public:
         virtual ~A() {}
    };
    

    without it you cannot safely write code like:

    m_collection.push_back( new B );
    delete m_collection[0];
    

    Doing this will also enable run-time type information. Howver, switching on typeid is not how C++ likes you to use RTTI – you should use dynamic_cast:

    m_collection.push_back( new B );    // or new A or new C
    if ( C * c = dynamic_cast<C *>( m_collection[0] ) ) {
       c->CFunc():  // function in C
    }
    else if ( B * b = dynamic_cast<B *>( m_collection[0] ) ) {
       b->BFunc():  // function in B
    }
    else if ( A * a = dynamic_cast<A *>( m_collection[0] ) ) {
       a->AFunc():  // function in A
    }
    else {
      throw "unknown type";
    }
    

    In general however, it is better to use the virtual function mechanism for dispatch, rather than RTTI.

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

Sidebar

Related Questions

I have a base class that I inherit from that has two zero to
I have two Parser classes that inherit from a base class, BaseParser. I want
I have a base class, B, which has two constructors, one with no paremeters
I have a class which is derived from a base class, and have many
I have written a base class from which I wish to derive several child
We have a set of about two dozen classes that inherit from a base
I have two classes: a base class, Foo::Base and a derived class, Foo::Base::Sub .
I have a base class called SpriteSheet which has two children, RotatedSpriteSheet and FlippedSpriteSheet.
I have a class that has two functions, both of which take a different
I have a two windows forms classes, a base class and a derived class.

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.