473,378 Members | 1,386 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,378 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 8780
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...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.