Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

traxxas25

run once

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

how do i run a line of code only once in visual C++?, i want to run this line once: form2*frm = new form2(); so the program doesnt keep creating new copies of that one form or if theres another way to do this tell me please thanks guys

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster

Well, a quick and dirty way (although, not necessarily the best way) is to use a static variable as a flag for initialization.

So, you would do something like this:

static bool initialized = false;

if (!initialized)
{
form2 *frm = new form2();
initialized = true;
}

The first time this code is run, frm will be set up, and on subsequent run throughs of the code, the stuff within the curly braces won''t be run.

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster

Well, a quick and dirty way (although, not necessarily the best way) is to use a static variable as a flag for initialization.

So, you would do something like this:

static bool initialized = false;

if (!initialized)
{
form2 *frm = new form2();
initialized = true;
}

The first time this code is run, frm will be set up, and on subsequent run throughs of the code, the stuff within the curly braces won''t be run.



That wouldn''t work too well since the form2 variable is only in scope within the if-statement.

Why not just:


static form2 *frm = new form2();


Then you don''t need the static boolean.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
that static form2 *frm = new form2(): doesnt work i get a compiler error

error C3145: ''frm'' : cannot declare a global or static managed type object or a __gc pointer

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
could you possibly explain that code and explain were to put it, its alittle confusing, thanks alot!

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!