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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T23:27:08+00:00 2026-06-01T23:27:08+00:00

I have the following classes in my c++ program: game_entity game_creature which inherits from

  • 0

I have the following classes in my c++ program:

  • game_entity
  • game_creature which inherits from game_entity

I plan to have more classes that either inherit from game_entity, game_creature, or even some other lower classes.

I also have an array of type game_entity, which contains both game_entity instances, and game_creature instances. Now my problem is this: I try to loop through the array when it is passed to a function, but when it gets to a game_creature object, it does not increment the pointer by enough using subscript form.

For example:

If the first object is a game_creature (which has more fields than a game_entity), it will access the first 3 fields (which are the same on game_creature as on game_entity) just fine, but when I do array[1], it will access the value of the second two ints in the first game_creature as the first two ints in the second game_creature object.

This would mean that if I have a game_creature with x = 1, y = 2, sprite* = Something, movespeed = 5, and health = 25, then it would access x and y of the second game_creature object as 5 and 25, which is actually the movespeed and health of the first game_creature.

I think this is because the memory that the pointer is pointing to for the x and y of a “game_entity” is actually where the movespeed and health of a “game_creature” are. However, I don’t know how to make an array of game_entities, so it can hold any type of object that would be on the grid of my game, but still have it access the proper fields and increment the pointer by enough each time.

Any help is appreciated.

  • 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-01T23:27:09+00:00Added an answer on June 1, 2026 at 11:27 pm

    You can create an array of pointers to your base class, and that will “hold” any of your entities, but you can’t create an array of the base class and assume it’ll hold your sub-classes as well, because in C/C++, arrays are fixed-length, and each element is a fixed amount. Even if you used a vector or something, it still wouldn’t work, as each element would be purely the base class.

    In C++11, you could use a tuple, but it’s probably not what you’re looking for. I’d use an array of pointers (or better yet, a container class of smart pointers) to the base class, and iterate through that.

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

Sidebar

Related Questions

I have the following program which compiles fine and with no errors or warnings:
I have three classes in my C# program. Get the following error: Cannot implicitly
I have a program complete with a number of classes which create complex objects,
In C++ I have the following two classes that I expose (using Boost) to
Suppose, I have following classes: public class DisposableObj : IDisposable { public ChildObj CreateObj();
I have the following classes in my models file class HardwareNode(models.Model): ip_address = models.CharField(max_length=15)
I have the following classes defined to do validation: public class DefValidator : IValidate<IDef>
I have the following classes in my ActiveRecord model: def Property < ActiveRecord::Base #
I have the following classes: class Vigil < ActiveRecord::Base after_update :do_something_cool private def do_something_cool
I have the following classes in my Model: public abstract class Entity : IEntity

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.