if (fire == true)
{
Projectile bullet(playerAngle);
}
bullet.move();
bullet.destroy();
Constructor and Scope Issue
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:
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).
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).
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement