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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:43:05+00:00 2026-05-11T13:43:05+00:00

Let’s say I’m creating an OpenGL game in C++ that will have many objects

  • 0

Let’s say I’m creating an OpenGL game in C++ that will have many objects created (enemies, player characters, items, etc.). I’m wondering the best way to organize these since they will be created and destroyed real-time based on time, player position/actions etc.

Here’s what I’ve thought of so far: I can have a global array to store pointers to these objects. The textures/context for these objects are loaded in their constructors. These objects will have different types, so I can cast the pointers to get them in the array, but I want to later have a renderObjects() function that will use a loop to call an ObjectN.render() function for each existing object.

I think I’ve tried this before but I didn’t know what type to initialize the array with, so I picked an arbitrary object type, then cast anything that wasn’t of that type. If I remember, this didn’t work because the compiler didn’t want me dereferencing the pointers if it no longer knew their type, even if a given member function had the same name: (*Object5).render() <-doesn’t work?

Is there a better way? How to commercial games like HL2 handle this? I imagine there must be some module etc. that keeps track of all the objects.

  • 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. 2026-05-11T13:43:06+00:00Added an answer on May 11, 2026 at 1:43 pm

    I’m not sure I fully understand the question but I think you are wanting to create a collection of polymorphic objects. When accessing a polymorphic object, you must always refer to it by a pointer or a reference.

    Here is an example. First you need to set up a base class to derive your objects from:

    class BaseObject { public:     virtual void Render() = 0; }; 

    Then create the array of pointers. I use an STL set because that makes it easy to add and remove members at random:

    #include <set>  typedef std::set<BaseObject *> GAMEOBJECTS; GAMEOBJECTS g_gameObjects; 

    To add an object, create a derived class and instantiate it:

    class Enemy : public BaseObject { public:     Enemy() { }     virtual void Render()     {       // Rendering code goes here...     } };  g_gameObjects.insert(new Enemy()); 

    Then to access objects, just iterate through them:

    for(GAMEOBJECTS::iterator it = g_gameObjects.begin();     it != g_gameObjects.end();     it++) {     (*it)->Render(); } 

    To create different types of object, just derive more classes from class BaseObject. Don’t forget to delete the objects when you remove them from the collection.

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

Sidebar

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.