468,512 Members | 1,402 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

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??
Dec 28 '07 #1
2 7096
On Dec 27, 10:21 pm, Mark <mnbaya...@gmail.comwrote:
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
Dec 28 '07 #2
On Dec 27, 11:57 pm, sonison.ja...@gmail.com wrote:
On Dec 27, 10:21 pm, Mark <mnbaya...@gmail.comwrote:
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.
Dec 29 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by johny smith | last post: by
4 posts views Thread by Martin Feuersteiner | last post: by
2 posts views Thread by Daniel Lidström | last post: by
4 posts views Thread by Peter Afonin | last post: by
4 posts views Thread by Edwin Gomez | last post: by
11 posts views Thread by briankirkpatrick | last post: by
47 posts views Thread by teju | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.