C++ create an array of nested class objects in a class

Started by
11 comments, last by rip-off 10 years, 10 months ago
Can this be done or am I clutching at straws?

I just want a class that uses the constructor to initialize an array of nested class objects.


class GenObjectClass
{
public:
class cTroopObj
{
public:
cTroopObj () { iTroopSize++; }
~cTroopObj () { iTroopSize--; }

void fInitObj();

private:
int miType,
miHealth,
miRally,
miSpeed,
miXloc,
miYloc,
miZloc;
};

private:
static int iTroopSize;

public:
static int siN; //keep track of objects
static cTroopObj mcaTroops[MAX_TROOPS];

void SetupObj();

GenObjectClass () { siN++; SetupObj(); }
~GenObjectClass () { siN--; }
};

void SetupObj()
{
//set up the troops
for (int i = 0; i < 4; ++i)
{
GenObjectClass::mcaTroops = new GenObjectClass::cTroopObj;
}
};
If you get near a point, make it!
Advertisement
You don't need SetupObj(). The objects in the arrays will be created when the array is created.

You forgot an asterisk (mcaTroops must be a pointer array): static cTroopObj* mcaTroops[MAX_TROOPS];

Other than that, I don't see why this wouldn't work. If you want to store objects instead of pointers, that will of course work too, but then you should not assign objects that you allocate on the heap (and you'd need an overloaded operator=).

And... that too (good catch):

You don't need SetupObj(). The objects in the arrays will be created when the array is created.

...if you want to create MAX_TROOPS objects in every GenObjectClass object, then just remove SetupObj.

I think this breaks some good programming guidelines...

You want the troops to be stored and created by an outside entity.

eg.

Troop

TroopContainer

*Container<Troop>

*addTroop(Troop troop)

o3o

Awesome thanks guys.

Just a few last questions, do I need to "delete mcaTroops;" in the destructor?
As soon as I use a pointer the memory is allocated on the heap isn't it, will it stay in scope whilst the GenObjectClass object; still exists!

And do I need to declare the object as a pointer to keep it 'alive' (GenObjectClass* object;) within the scope of the main function? deleting that when it is also not needed in the code.
If you get near a point, make it!
I want an array of nested class objects to be stored in the main class objects array, they are then created and detroyed by the function calls of the class they are nested in.

Troop
TroopContainer
*Container<Troop>
*addTroop(Troop troop)

If you get near a point, make it!
You should only delete what you have created with new. Is mcaTroops supposed to be static, meaning it only exists one such array that will stay alive until the end of the program, or do you want each GenObjectClass to have its own mcaTroops array?

You should only delete what you have created with new.

So for the following line how do I reference back to this object once created? or do I just assign the array slot to zero?
GenObjectClass::mcaTroops = new GenObjectClass::cTroopObj;

Is mcaTroops supposed to be static, meaning it only exists one such array that will stay alive until the end of the program, or do you want each GenObjectClass to have its own mcaTroops array?

Ah I seem to have mistaken the use of static then, I didn't realise it was till program death, I thought it was in the scope of the objects lifespan.

yes, each GenObjectClass to have its own mcaTroops array.
If you get near a point, make it!

It looks as though you're trying to create a horrific mashup between a freelist and a monostate? There are reasons why I could see it being useful in some high performance contexts, or some really underpowered architectures, but I suspect this will end badly in this case (static variables are just globals, and that's usually always a bad idea). The biggest problem you'll face is that you're going to implicitly tie together the implementations of a few classes, and code reuse will go flying out the window. But anyway....

The easiest way would be like this:


#include <vector>
 
class cTroops
{
public:
 
  class cTroopObj
  {
    friend class cTroops;
    cTroopObj(int type /* add more construction args if needed */ ) 
      : miType(type), miHealth(100) /* initialise remaining member vars here */ { }
    ~cTroopObj() {}
  public:
 
    /* member funcs here */
 
  private:
    int miType,
      miHealth,
      miRally,
      miSpeed,
      miXloc,
      miYloc,
      miZloc;
  };
 
  cTroups() : m_troups() {}

  void addTroup(int type /* more args if needed */) 
  {
    m_troups.push_back( cTroopObj(type /* more args if needed */) );
  }
 
  void removeTroup(size_t index)
  {
    m_troups.erase(m_troups.begin() + index);
  }
 
  cTroopObj& operator [] (size_t index) 
  {
    return m_troups[index];
  }
 
  const cTroopObj& operator [] (size_t index) const 
  {
    return m_troups[index];
  }
 
  size_t size() const 
  {
    return m_troups.size();
  }
 
private:
  std::vector<cTroopObj> m_troups;
};
 
// to use ....
cTroups troups;
 
troups.addTroup( 1 );
troups.addTroup( 2 );
troups.addTroup( 3 );
 
for(size_t i = 0; i < troups.size(); ++i)
{
  troups.someMemberFuncOrWhatever();
}
troups.removeTroup( 0 ); // remove first troup
 
// remaining troups will be destroyed when troups goes out of scope

/edit what the hell is wrong with gamedev? Why are tabs being stripped from everything?
/edit3 And newlines as well? WTF????
/edit7094 Ok, so code tags strip tabs and spaces. Source tags strip newlines. Wut?

/edit7075 F**k it. Have the screwed version.

That's great ta:

Firstly I was just trying to encapsulate the code as it seemed the natural progression if I were creating objects in objects. Is it wrong to try this kind of nesting? Would I be better off just creating a separate class?

Secondly I was trying to avoid vectors as I thought that you couldn't save them in .bin files. only vars and arrays! Is that a falsehood?
If you get near a point, make it!

This topic is closed to new replies.

Advertisement