Constructor and Scope Issue

Started by
1 comment, last by X Abstract X 15 years, 8 months ago
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();

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).
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]

This topic is closed to new replies.

Advertisement