473,372 Members | 1,352 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

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 8775
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Red Ogden | last post by:
The following script returns an error saying document.all is null or not an object when I try to call the write_layer function more than once within the same else statement i.e.: else {...
12
by: johny smith | last post by:
I am trying to figure out a way to print the address of what called a certain function once inside the function. I am assuming that this information is on the stack somewhere. But can someone...
9
by: kernelxu | last post by:
hi,everybody. I calling function setbuf() to change the characteristic of standsrd input buffer. some fragment of the progrem is: (DEV-C++2.9.9.2) #include <stdio.h> #include <stdlib.h> int...
4
by: Martin Feuersteiner | last post by:
Dear Group I'm using VB to write an aspnet application. I would like to call a javascript function from within a VB Sub or VB Function, is it possible? My code is something like this: VB...
0
by: gfricke | last post by:
I am calling my synchronus webservice using the .onserviceavailable set to my calling Jscript function which works great. Then I click the button again that calls my function which sets the...
2
by: Daniel Lidström | last post by:
I'm using a library called fyba. This library reads and writes files in a format called sosi. fyba uses the following code to determine if the calling process has own methods to handle errors,...
4
by: Peter Afonin | last post by:
Hello, I have a weirdest issue I've ever had. I have a function that enters some data into the Oracle table and returns the sequential row number for the new record (autonumber): Private...
4
by: Edwin Gomez | last post by:
I'm a C# developer and I'm new to Python. I would like to know if the concept of Asynchronous call-backs exists in Python. Basically what I mean is that I dispatch a thread and when the thread...
11
by: briankirkpatrick | last post by:
Forgive me if my post seems a little amateurish... I'm requesting assistance from some of you smart folks out there to get the managed calls write that meet the specification in the esa.h for...
47
by: teju | last post by:
hi, i am trying 2 merge 2 projects into one project.One project is using c language and the other one is using c++ code. both are working very fine independently.But now i need to merge both...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.