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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T16:49:59+00:00 2026-06-03T16:49:59+00:00

In my program I have a class named Entity. Another class Invader inherits Entity.

  • 0

In my program I have a class named Entity. Another class Invader inherits Entity. Since I want to have 4 different kinds of invaders, I declare classes Invader1, Invader2, Invader3 and Invader4 which inherit from Invader. Now I declare a Entity pointer type vector to store all Invaders like:

entities.push_back(new Invader4());
entities.push_back(new Invader3());
entities.push_back(new Invader2());
entities.push_back(new Invader1());
entities.push_back(new Invader0());

When I check the type of the element in entities at runtime, say

typeid(*entities->at(index)) 

It may return one of the 4 kinds of invaders. In entities there are also other objects which inherit Entity. So i just want to check if the type of the object is Invader or not, I don’t want to know if it is Invader1, Invader2, etc.

How I can achieve this?

  • 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-03T16:50:01+00:00Added an answer on June 3, 2026 at 4:50 pm

    There will be many ways to do this in C++, but the fundamental problem is that once you have to start querying elements in a container that is supposed to be polymorphic you might as well give up on the idea of using polimorphism. The whole point of having collections of polymorphic elements is that you can treat them all the same. So if you have a vector<Entity*> you should only treat its elements as Entity*s. If you find that you need to call some Invader-like functions on an Entity, then you are better off holding a container of Invader* too (using the same pointers as for the original container).

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

Sidebar

Related Questions

I have a class called ModelView which inherits from NSOpenGLView. When my program runs
I have a main class in a program that launches another class that handles
So assume I have one class named Program which contains my whole program of
I have run into a small issue in my program. I have a class
I am using boost shared pointers in my program, and I have a class
I have a program with a class that contains a public enum, as follows:
I have a program for a C class I need to write. The program
I have a program that looks something like this: public partial class It {
I have a DAL class library that is included in my program as a
I have a program in which I need to store a Class object into

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.