Just once, please!
I''m just wondering and sorta taking a shot in the dark here. Is there a keyword in C++ that, when placed before a line of code (like #define) makes it so the line of code only executes once and is then ignored? I don''t think so cause I''ve never seen it used, but if so, it would be a lot better than having to create a stupid global or static variable called oncethru or something like that to make sure the line(s) aren''t executed again. Ah well, here''s hopin
==============================
"Need more eeenput..."
- #5, "Short Circuit"
==============================
Drew Sikora
Executive Producer
GameDev.net
Try
~Neophyte
- Death awaits you all with nasty, big, pointy teeth. -
Edited by - Neophyte on November 9, 2000 2:27:38 PM
#ifndef __SOMETHING_OR_OTHER__#define __SOMETHING_OR_OTHER__// Your code here#endif
~Neophyte
- Death awaits you all with nasty, big, pointy teeth. -
Edited by - Neophyte on November 9, 2000 2:27:38 PM
November 09, 2000 01:42 PM
Neophyte: That won''t accomplish what he''s asking for...
That will only be useful if the file where it is written is #include''d multiple times in another file. Then it will cause the compiler to ignore every inclusion (of those particular lines) but the first one.
what I believe he/she is looking for is something like:
and the ''once''-block would only be executed one time ever....
There is however no such structure in C/C++
You have to use a variable to remember if you have executed the code previously or not...
That will only be useful if the file where it is written is #include''d multiple times in another file. Then it will cause the compiler to ignore every inclusion (of those particular lines) but the first one.
what I believe he/she is looking for is something like:
// fictitious code:once { do_something_here();};
and the ''once''-block would only be executed one time ever....
There is however no such structure in C/C++
You have to use a variable to remember if you have executed the code previously or not...
No. There is no such thing in C++. Interesting, but in general when you only want something to be executed once, you can simply call it once.
cmaker
- its not the principle. its the money.
cmaker
- its not the principle. its the money.
cmaker- I do not make clones.
Try this:
// In the function you only want to run once do this
void myfunc()
{
static short runonce = 0;
if ( runonce == 1 )
return;
runonce = 1;
.
.
.
}
// In the function you only want to run once do this
void myfunc()
{
static short runonce = 0;
if ( runonce == 1 )
return;
runonce = 1;
.
.
.
}
--==JEEDIO==--
Thank you, jeedio, obviously you didn''t read well enough to realize i said i didn''t want to do those stupid "oncethru" variables. But... *sigh* it seems I have no choice. And CMaker (how ya doin there good buddy.. he heh ), I can''t call it once cause this code goes in a system timer message handler. Oh well, guess its that static variable route once again. Thanks guys.
==============================
"Need more eeenput..."
- #5, "Short Circuit"
==============================
==============================
"Need more eeenput..."
- #5, "Short Circuit"
==============================
Drew Sikora
Executive Producer
GameDev.net
There is no C/C++ language construct to do what you want. Best thing you can do is package the thing up as nicely as possible. Here's one idea:
This is an untested and not terribly safe solution, but should work if used carefully. The ONCE and NTIMES macros are somewhat unguarded; what I mean is that if used in the wrong place will cause strange and bewildering errors that will be a pain to catch.
The TMP creates temporary variables names. It has the advantage that I can create unique, hidden variable names very easy; the name provided need only be unique on a line per line basis. The disadvantage is that you can only use that variable on the one line, since there's no way to keep track of it. It may not even be safe on one line, depending on what other macros you might use with it. In any case, as provided above, it should be safe enough, but at least I've warned you.
Of course, if anyone has improvements to the above, speak up!
---- --- -- -
Blue programmer needs food badly. Blue programmer is about to die!
---- --- -- -
New York. New York. New York. Texas. Texas. New York. New York. Canada.
---- --- -- -
Edited by - mossmoss on November 10, 2000 9:00:47 AM
class N_Times {private: int n;public: N_Times(int nt=1) : n(nt) { } operator bool() const { if (n > 0) { --n; //only do when positive so we never underflow return true; } return false; }}; #define TMP(x) x##__LINE__#define ONCE static N_Times TMP(once); if TMP(once)#define NTIMES(n) static N_Times TMP(ntimes)(n); if TMP(ntimes) //sample usagevoid foo(){ //...do some code here... ONCE { //...do once only code here... } //...do other code here... NTIMES(12) { //...this code will be done twelve times... }}
This is an untested and not terribly safe solution, but should work if used carefully. The ONCE and NTIMES macros are somewhat unguarded; what I mean is that if used in the wrong place will cause strange and bewildering errors that will be a pain to catch.
The TMP creates temporary variables names. It has the advantage that I can create unique, hidden variable names very easy; the name provided need only be unique on a line per line basis. The disadvantage is that you can only use that variable on the one line, since there's no way to keep track of it. It may not even be safe on one line, depending on what other macros you might use with it. In any case, as provided above, it should be safe enough, but at least I've warned you.
Of course, if anyone has improvements to the above, speak up!
---- --- -- -
Blue programmer needs food badly. Blue programmer is about to die!
---- --- -- -
New York. New York. New York. Texas. Texas. New York. New York. Canada.
---- --- -- -
Edited by - mossmoss on November 10, 2000 9:00:47 AM
quote: Original post by Gaiiden
Thank you, jeedio, obviously you didn''t read well enough to realize i said i didn''t want to do those stupid "oncethru" variables. But... *sigh* it seems I have no choice. And CMaker (how ya doin there good buddy.. he heh ), I can''t call it once cause this code goes in a system timer message handler. Oh well, guess its that static variable route once again. Thanks guys.
==============================
"Need more eeenput..."
- #5, "Short Circuit"
==============================
Ah, I see. Still, the reasoning behind people designing these languages is that "if that code only needs to be called once why would it be in a message handler?" (even though this could be the best way to do it). Anyway, I''m doing excellent(okay). Thank you.
cmaker- I do not make clones.
Despite the utility of such a language construct, there is some ambiguity here.
Think of this:
while (whatever) {
do_stuff ();
once {
do_more_stuff ();
}
}
Now, is it your intention that do_more_stuff () is executed once per running of the program, once during the calling of the function that contains the while loop, or once during execution of the while loop?
Think of this:
while (whatever) {
do_stuff ();
once {
do_more_stuff ();
}
}
Now, is it your intention that do_more_stuff () is executed once per running of the program, once during the calling of the function that contains the while loop, or once during execution of the while loop?
_______________________________
"To understand the horse you'll find that you're going to be working on yourself. The horse will give you the answers and he will question you to see if you are sure or not."
- Ray Hunt, in Think Harmony With Horses
ALU - SHRDLU - WORDNET - CYC - SWALE - AM - CD - J.M. - K.S. | CAA - BCHA - AQHA - APHA - R.H. - T.D. | 395 - SPS - GORDIE - SCMA - R.M. - G.R. - V.C. - C.F.
"To understand the horse you'll find that you're going to be working on yourself. The horse will give you the answers and he will question you to see if you are sure or not."
- Ray Hunt, in Think Harmony With Horses
ALU - SHRDLU - WORDNET - CYC - SWALE - AM - CD - J.M. - K.S. | CAA - BCHA - AQHA - APHA - R.H. - T.D. | 395 - SPS - GORDIE - SCMA - R.M. - G.R. - V.C. - C.F.
#define Once(Statement, Name) static int __ONCE_##Name = 0; if (__ONCE_##Name == 0) { Statement; } \ __ONCE_##Name++;
should work, just send the thing you want to exec. as Statement, and a comment type name, like this
Once (cout << "Hello", PrintHello);
farmersckn
Edited by - farmersckn on November 11, 2000 2:09:36 AM
Yesterday is the past, tomorrow is the future. Today is a gift, that is why we call it the present.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement