Calling a function only once



 DEVELOP > c-Plus-Plus > Calling a function only once

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Mark"
Date: 28 Dec 2007 12:21:13 AM
Object: Calling a function only once
This is just out of curiosity...
There are a couple ways to make sure a function gets called only once.
Let's say I have a function that gets called several times, called
LoopedFunc() and I have another function called Init() which I only
want to run once... then I can do
LoopedFunc()
{
static bool initialized = false;
if(!initialized) {
Init();
initialized = true;
}
}
or...I can make my Init() function return a boolean and then go
LoopedFunc()
{
static bool this_variable_is_never_used = Init();
}
The second way only requires one line...but still requires a pretty
useless variable, and forces you to put a return on your Init
function.
Shouldn't there be a way to do something like
LoopedFunc()
{
static Init();
}
or something??
.

User: ""

Title: Re: Calling a function only once 28 Dec 2007 01:57:56 AM
On Dec 27, 10:21 pm, Mark <mnbaya...@gmail.com> wrote:

This is just out of curiosity...

There are a couple ways to make sure a function gets called only once.
Let's say I have a function that gets called several times, called
LoopedFunc() and I have another function called Init() which I only
want to run once... then I can do

LoopedFunc()
{
static bool initialized = false;
if(!initialized) {
Init();
initialized = true;
}

}

or...I can make my Init() function return a boolean and then go

LoopedFunc()
{
static bool this_variable_is_never_used = Init();

}

The second way only requires one line...but still requires a pretty
useless variable, and forces you to put a return on your Init
function.

Shouldn't there be a way to do something like

LoopedFunc()
{
static Init();

}

or something??

A crude way of doing it could be...
class Init
{
public:
Init();
};
LoopedFunc()
{
static Init init;
}
Thanks and regards
Sonison James
.
User: "Mark"

Title: Re: Calling a function only once 29 Dec 2007 05:08:26 PM
On Dec 27, 11:57 pm,
wrote:

On Dec 27, 10:21 pm, Mark <mnbaya...@gmail.com> wrote:



This is just out of curiosity...


There are a couple ways to make sure a function gets called only once.
Let's say I have a function that gets called several times, called
LoopedFunc() and I have another function called Init() which I only
want to run once... then I can do


LoopedFunc()
{
static bool initialized = false;
if(!initialized) {
Init();
initialized = true;
}


}


or...I can make my Init() function return a boolean and then go


LoopedFunc()
{
static bool this_variable_is_never_used = Init();


}


The second way only requires one line...but still requires a pretty
useless variable, and forces you to put a return on your Init
function.


Shouldn't there be a way to do something like


LoopedFunc()
{
static Init();


}


or something??


A crude way of doing it could be...

class Init
{
public:
Init();

};

LoopedFunc()
{
static Init init;

}

Thanks and regards
Sonison James

Ahh..that's even worse in my books :p Oh well. I was just wondering.
.



  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER