Let's say I have a class that includes an array of some struct, and a pointer to that array.
struct Box {
//stuff
};
class Foo {
private:
Box *boxPtr //pointer to an array of Box structs
int max; //the size of the partially filled array
int boxCounter; //the current number of non-NULL elements in the array
public:
Foo(); //constructor
Foo(const Foo &obj); //copy constructor
~Foo(); //destructor
bool newBoxInsert(Box newBox){
//adds another Box to my array of Box structs
boxCounter++;
}
//etc
};
and in my int main(), I somehow must create a brand new object of class Foo. I'm going to need to partially fill that array of indeterminate size, whose pointer is boxPtr.
How would I go about initializing that array? Should the constructor do it? Or should I let newBoxInsert handle it?
In either case, how would I achieve that? I'm guessing I would have to dynamically allocate the array. If that's the case, then it's good to have the pointer as a class member... right?
For example, when adding the very first element to my array, should I use
boxCounter = 1;
boxPtr = new Box[boxCounter];
then continue on to keep adding elements to the array?
Perhaps this is just better done with vectors. They're much more... flexible (?) when adding elements. Can vectors contain structs as elements?
[/n00b]