Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

oxygen_728

C++ - How do I have a variable sized array of objects...

This topic is 5295 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

I want to prototype an array of CBALL objects as: class CBuilding{ ... CBall* Balls ... or CBall** Balls (or however it is done.) ... } Then, in my CBuilding class''s constructor... Balls = Array of Balls of size MAX_BALLS_IN_BUILDING MAX_BALLS_IN_BUILDING is #define''d as a certain integer. ---- My primary concern... What is the proper way to put a new instance of a CBALL object in each position in the array? Thank you for the help!

Share this post


Link to post
Share on other sites
Advertisement

class CBuilding
{
public:
CBuilding(int numBalls = MAX_BALLS_IN_BUILDING);
private:
CBall *Balls;
}

CBuilding::CBuilding(int numBalls)
{
Balls = new CBall[numBalls];
}


Share this post


Link to post
Share on other sites
If you need a destructor, you probably need a copy-constructor and assignment operator, too. Don''t forget them either.
The easy way is to use a std::vector.

class CBuilding
{
public:
CBuilding(int numBalls = MAX_BALLS_IN_BUILDING);
private:
std::vector<CBall> Balls;
}

CBuilding::CBuilding(int numBalls)
{
Balls.resize(numBalls);
}

Share this post


Link to post
Share on other sites
quote:
Original post by bobstevens
Do NOT forget your destructor:
CBuilding::~CBuilding()
{
delete [] Balls;
}



Yes... quite right. Thanks for pointing that out... i forgot to include it in my original soln.

Share this post


Link to post
Share on other sites

  • 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!