Jump to content
  • Advertisement
Sign in to follow this  
chadsxe

How do you destroy variables that are still in scope

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

My ap has diffrent states. Each state uses diffrent variables. Instead loading them all at the start of the program, I was wondering how to destroy them the ones that are needed any more as I move from one state to another. My question would span variables and class objects (i.e. SomeClass someclass). Any suggestions? Regards Chad

Share this post


Link to post
Share on other sites
Advertisement
You don't, unless you allocated them yourself via new. If you did, you can kill them off whenever you feel like it by deleting them. However, everything else is controlled by its scope, and you don't want to mess with that or you'll cause undefined behavior.

Care to provide a more in-depth example? If each state is modelled as a different class, you only need to create and destroy an instance of the class to effectively control the lifetime of the variables in that class. There is no need to allocate them all at startup, necessarily.

Share this post


Link to post
Share on other sites
create blocks for the variables in question to dictate their scope at compile time.

example.

function a() {
int x=6;
{
int x=3;
std::cout << x << std::endl;
}
std::cout << x << std::endl;
}

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!