Jump to content
  • Advertisement
Sign in to follow this  
Ubermeowmix

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

This topic is 1945 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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;
}
};

Share this post


Link to post
Share on other sites
Advertisement
You don't need SetupObj(). The objects in the arrays will be created when the array is created.

Share this post


Link to post
Share on other sites

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.

Edited by samoth

Share this post


Link to post
Share on other sites

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)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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)

Edited by Ubermeowmix

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites

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. Edited by Ubermeowmix

Share this post


Link to post
Share on other sites

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.

Edited by RobTheBloke

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!