PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.cplus > Calling a function only once
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Calling a function only once

Réponse
 
LinkBack Outils de la discussion
Vieux 28/12/2007, 06h21   #1
Mark
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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??
  Réponse avec citation
Vieux 28/12/2007, 07h57   #2
sonison.james@gmail.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Calling a function only once

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
  Réponse avec citation
Vieux 29/12/2007, 23h08   #3
Mark
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Calling a function only once

On Dec 27, 11:57 pm, sonison.ja...@gmail.com 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.
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 11h26.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,10264 seconds with 11 queries