• Advertisement
Sign in to follow this  

Constructor and Scope Issue

This topic is 3554 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 working on a space shooter type game in c++ with SDL and I'm having difficulty implementing projectiles. Right now I have a Projectile class that has a constructor, move function and destroy functions. I'll provide an example rather than trying to explain, but the problem is after I construct my projectile I'm unable to call the move and destroy functions because of a scope limitation and I need to find a way around this. Code from main function:
if (fire == true)
{
    Projectile bullet(playerAngle);
}

bullet.move();
bullet.destroy();

Share this post


Link to post
Share on other sites
Advertisement
I'm not sure calling this a "scope limitation" captures the essence of the issue; the bullet doesn't exist beyond the '}' (its destructor is called).

One way to get around this is to create bullet on the heap instead of on the stack. There are other ways (e.g. you could create a collection of Projectile objects, manage their lifetimes, and reuse them).

Share this post


Link to post
Share on other sites
Thanks alot for pointing me in the right direction.

Edit: After a bit of thinking you made me realise I wasn't even heading in the direction I had planned to take.

[Edited by - X Abstract X on August 2, 2008 12:16:37 AM]

Share this post


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

  • Advertisement