Jump to content
  • Advertisement
Sign in to follow this  
Kalasjniekof

trouble with new keyword

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

Hi. I'm currently implementing a very basic particle system. Particles are represented by the CParticle class. Particles belong to a CParticleSystem and are stored in a single pool, so no creating/destroying particles is needed while the system is running. When a particlesystem is created it will create int m_numparticles of particles. The particles are stored in a plain array as I wont need to resize it constantly. I've tried creating the array like this:
    m_particles = NULL;

    // create array of m_numparticles. 'this'is a pointer to the parentsystem.
    // CParticle(CParticleSystem *a_system) is a constructor of CParticle.
    m_particles = new CParticle(this) [m_numparticles]; 
but it doesn't work. The compiler gives me this error. error: expected ';' before '[' token Does anyone know what is wrong?

Share this post


Link to post
Share on other sites
Advertisement

m_particles = new CParticle[m_numparticles];

for(int i = 0; i < m_numparticles; i++)
{
m_particles = new CParticle(this);
}




later edit: assuming CParticle** m_particles [wink]

Share this post


Link to post
Share on other sites
You can't pass arguments to the constructor of elements of an array being created dynamically. Consider using std::vector which will allow you to specify an object to copy construct the elements from.

Share this post


Link to post
Share on other sites
To be a little more explicit:


std::vector<CParticle> m_particles(m_numparticles, CParticle(this));
// the temporary, stack-allocated CParticle is copy-constructed to each
// element of the vector. The vector "knows" its size, so m_numparticles
// will become redundant. You will of course need a proper copy constructor
// and destructor (and therefore assignment operator) for the CParticle -
// depending on what exactly is in there, the defaults may be ok.

Share this post


Link to post
Share on other sites
I guess I'll use werekarg's approach, as I don't want to use a vector for an array that will only change it's size once. Thanks all of you though.

Share this post


Link to post
Share on other sites
That's a weak argument. Using a vector is just as efficient as using an array created with new. Sure, it contains a few extra bytes used for it's internal book keeping, but you dragging around that m_numparticles variable isn't a whole lot better.

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!