C++ create an array of nested class objects in a class
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;
}
};
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)
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.
Troop
TroopContainer
*Container<Troop>
*addTroop(Troop troop)
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?You should only delete what you have created with new.
GenObjectClass::mcaTroops = new GenObjectClass::cTroopObj;
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.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?
yes, each GenObjectClass to have its own mcaTroops array.
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.
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?