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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:31:37+00:00 2026-05-14T02:31:37+00:00

I’m trying to expand my sons interest from Warcraft 3 programming into C++ to

  • 0

I’m trying to expand my sons interest from Warcraft 3 programming into C++ to broaden his horizons to a degree. We are planning on porting a little game that he wrote.

The context goes something like this.

  • There are Ships and Missiles, for which Ships will use Missiles and interact with them
  • A Container exists which will hold ‘a list’ of ships.
  • A Container exists which will hold ‘a list’ of planets.
  • One can apply a function over all elements in the Container (for_each)
  • Ships and Missles can be created/destroyed at any time
  • New objects automatically insert themselves into the proper container.

I cobbled a small example together to do that job, so we can talk about topics (list, templates etc) but I am not pleased with the results.

#include <iostream>
#include <list>
using namespace std;

/* Base class to hold static list in common with various object groups */ 
template<class T>
class ObjectManager
{
    public :
        ObjectManager(void)
        {
            cout << "Construct ObjectManager at " << this << endl;

            objectList.push_back(this);
        }

        virtual ~ObjectManager(void)
        {
            cout << "Destroy ObjectManager at " << this << endl;
        }

        void for_each(void (*function)(T *))
        {
            for (objectListIter = objectList.begin(); 
                 objectListIter != objectList.end(); 
                 ++objectListIter)
            {
                (*function)((T *) *objectListIter);
            }
        }
               list<ObjectManager<T> *>::iterator objectListIter;
        static list<ObjectManager<T> *>           objectList;
};

/* initializer for static list */ 
template<class T>
list<ObjectManager<T> *> ObjectManager<T>::objectList;


/* A simple ship for testing */ 
class Ship : public ObjectManager<Ship>
{
    public :
        Ship(void) : ObjectManager<Ship>()
        {
            cout << "Construct Ship at " << this << endl;
        }

        ~Ship(void)
        {
            cout << "Destroy Ship at " << this << endl;
        }

        friend ostream &operator<<(ostream    &out,const Ship &that)
        {
            out << "I am a ship";

            return out;
        }
};

/* A simple missile for testing */ 
class Missile : public ObjectManager<Missile>
{
    public :
        Missile(void) : ObjectManager<Missile>()
        {
            cout << "Construct Missile at " << this << endl;
        }

        ~Missile(void)
        {
            cout << "Destroy Missile at " << this << endl;
        }

        friend ostream &operator<<(ostream &out,const Missile &that)
        {
            out << "I am a missile";

            return out;
        }
};

/* A function suitable for the for_each function */ 
template <class T>
void show(T *it)
{
    cout << "Show: " << *it << " at " << it << endl;
}

int main(void)
{
    /* Create dummy planets for testing */ 
    Missile p1;
    Missile p2;

    /* Demonstrate Iterator */ 
    p1.for_each(show);

    /* Create dummy ships for testing */ 
    Ship s1;
    Ship s2;
    Ship s3;

    /* Demonstrate Iterator */ 
    s1.for_each(show);

    return 0;
}

Specifically, The list is effectively embedded in each ship though the inheritance mechanism. One must have a ship, in order to access the list of ships. One must have a missile in order to be able to access the list of missiles. That feels awkward.

My question boils down to “Is there a better way to do this?”

  1. Automatic object container creation
  2. Automatic object insertion
  3. Container access without requiring an object in the list to access it.

I am looking for better ideas.
All helpful entries get an upvote.

Thanks

Evil.

  • 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-14T02:31:38+00:00Added an answer on May 14, 2026 at 2:31 am

    I think the problem here is that you are stretching the notion of inheritance. What you have is valid code and works but as you point out it doesn’t feel right to have a container class hidden behind the scenes of the constructor of your child class. This is (in my opinion) because a Missile doesn’t really have anything in common with it’s parent class (a container), you are using inheritance to provide this container when this is a design pattern that has (rightly) been abstracted out in the standard template library. As John Zwinck suggests a Factory pattern will provide a much more expressive way of achieving what you want.

    /* Container class to create and store various object groups */ 
    template<class T>
    class ObjectManager
    {
    public :
        ObjectManager() : nextAvailableIndex(0)
        {}
    
        virtual ~ObjectManager(void)
        {
            for (objectListIter = objectList.begin(); objectListIter != objectList.end(); ++objectListIter)
                delete *objectListIter;
        }
    
        T* ObjectManager::Create()
        {
            T* object = new Object(nextAvailableIndex++); 
            cout << "Constructed object " << object << endl;
            objectList.push_back(object);
        }
    
        void for_each(void (*function)(T *))
        {
            for (objectListIter = objectList.begin(); objectListIter != objectList.end(); ++objectListIter)
            {
                (*function)((T *) *objectListIter);
            }
        }
    
        int                nextAvailableIndex;
        list<T*>::iterator objectListIter;
        list<T*>           objectList;
    };
    
    int main(void)
    {
        /* Create dummy planets for testing */ 
        ObjectManager<Missile> missileManager;
        Missile* p1 = missileManager.Create();
    
        /* Demonstrate Iterator */ 
        missileManager.for_each(show);
    
        /* Create dummy ships for testing */ 
        ObjectManager<Ship> shipManager;
        shipManager.Create(); 
        shipManager.Create(); 
        shipManager.Create(); 
    
        /* Demonstrate Iterator */ 
        shipManager.for_each(show); // will show three ships
    
        return 0;
    }
    

    I’ve left out the class definitions for Missile and Ship as the only change I put there is a single integer to the constructors to functions as a unique identifier.

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

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Seemingly simple, but I cannot find anything relevant on the web. What is the
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and

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.