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 > Static variable & static method
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Static variable & static method

Réponse
 
LinkBack Outils de la discussion
Vieux 06/06/2008, 11h20   #1
Stefan Istrate
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Static variable & static method

Hello,
I have the following code and I still don't know why it prints "10 10"
instead of "5 10".

#include <iostream>
using namespace std;

class A {
static int i;
public:
static int get_i() {
return i;
}
static int dbl() {
i = i * 2;
return i;
}
};

int A::i = 5;

int main() {
cout << A::get_i() << " " << A::dbl() << endl;
return 0;
}

Can anyone me understand this?
Thank you,
Stefan Istrate
  Réponse avec citation
Vieux 06/06/2008, 11h34   #2
Erik Wikström
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Static variable & static method

On 2008-06-06 12:20, Stefan Istrate wrote:
> Hello,
> I have the following code and I still don't know why it prints "10 10"
> instead of "5 10".
>
> #include <iostream>
> using namespace std;
>
> class A {
> static int i;
> public:
> static int get_i() {
> return i;
> }
> static int dbl() {
> i = i * 2;
> return i;
> }
> };
>
> int A::i = 5;
>
> int main() {
> cout << A::get_i() << " " << A::dbl() << endl;
> return 0;
> }
>
> Can anyone me understand this?


It is because of the order in which the arguments are evaluated, the
call to A::dbl() will be evaluated before the call to A::get().

--
Erik Wikström
  Réponse avec citation
Vieux 06/06/2008, 11h40   #3
Stefan Istrate
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Static variable & static method

On Jun 6, 1:34pm, Erik Wikström <Erik-wikst...@telia.com> wrote:
> It is because of the order in which the arguments are evaluated, the
> call to A::dbl() will be evaluated before the call to A::get().
>
> --
> Erik Wikström


But the operator << is associative from left to right.

Stefan Istrate
  Réponse avec citation
Vieux 06/06/2008, 11h56   #4
Markus Moll
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Static variable & static method

Hi

Stefan Istrate wrote:

> On Jun 6, 1:34Âpm, Erik Wikström <Erik-wikst...@telia.com> wrote:
>> It is because of the order in which the arguments are evaluated, the
>> call to A::dbl() will be evaluated before the call to A::get().
>>
>> --
>> Erik Wikström

>
> But the operator << is associative from left to right.


Doesn't matter.
Associativity only defines that the individual operator<< calls are grouped
left to right, i.e. that the expression

a << b << c

is parsed as (a << b) << c and not as a << (b << c). However, in the
expression (a << b) << c it is not defined whether c is evaluated before (a
<< b) or whether a is evaluated before b. A valid evaluation order could
even be: a, c, b, a << b, (a << b) << c.

Markus

  Réponse avec citation
Vieux 06/06/2008, 12h00   #5
Michael DOUBEZ
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Static variable & static method

Stefan Istrate a écrit :
> On Jun 6, 1:34 pm, Erik Wikström <Erik-wikst...@telia.com> wrote:
>> It is because of the order in which the arguments are evaluated, the
>> call to A::dbl() will be evaluated before the call to A::get().

>
> But the operator << is associative from left to right.


That doesn't mean there is a sequence point at each <<, the compiler is
free to evaluates the parameters in any order.

--
Michael
  Réponse avec citation
Vieux 06/06/2008, 12h08   #6
Stefan Istrate
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Static variable & static method

So I should never modify a variable used more than once during
evaluating an expression.
  Réponse avec citation
Vieux 06/06/2008, 13h01   #7
Michael DOUBEZ
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Static variable & static method

Stefan Istrate a écrit :
> So I should never modify a variable used more than once during
> evaluating an expression.


Yes, unless you have a sequence point (like before the ? in
<bool>?<op>:<op> or for *non-overloaded* &&, || or ',' operators).

See wikipedia by example for more information:
http://en.wikipedia.org/wiki/Sequence_point
Or the FAQ of comp.lang.c:
http://c-faq.com/expr/seqpoints.html

--
Michael
  Réponse avec citation
Vieux 06/06/2008, 18h55   #8
utab
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Static variable & static method

On 6 jun, 12:20, Stefan Istrate <stefan.istr...@gmail.com> wrote:
> Hello,
> I have the following code and I still don't know why it prints "10 10"
> instead of "5 10".
>
> #include <iostream>
> using namespace std;
>
> class A {
> static int i;
> public:
> static int get_i() {
> return i;
> }
> static int dbl() {
> i = i * 2;
> return i;
> }
>
> };
>
> int A::i = 5;
>
> int main() {
> cout << A::get_i() << " " << A::dbl() << endl;
> return 0;
>
> }
>
> Can anyone me understand this?
> Thank you,
> Stefan Istrate


Others have clarified that but there is one more subtle point to
state, maybe. The function calls can have some side effects, such as
throwing exceptions and so on. Therefore, it is best to keep the
output operations seperate from the function calls. Some experinced
will comment on this maybe...
  Réponse avec citation
Vieux 07/06/2008, 09h25   #9
James Kanze
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Static variable & static method

On Jun 6, 7:55 pm, utab <umut.ta...@gmail.com> wrote:
> On 6 jun, 12:20, Stefan Istrate <stefan.istr...@gmail.com> wrote:
> > I have the following code and I still don't know why it
> > prints "10 10" instead of "5 10".


> > #include <iostream>
> > using namespace std;


> > class A {
> > static int i;
> > public:
> > static int get_i() {
> > return i;
> > }
> > static int dbl() {
> > i = i * 2;
> > return i;
> > }
> > };


> > int A::i = 5;


> > int main() {
> > cout << A::get_i() << " " << A::dbl() << endl;
> > return 0;
> > }


> > Can anyone me understand this?


> Others have clarified that but there is one more subtle point
> to state, maybe. The function calls can have some side
> effects, such as throwing exceptions and so on. Therefore, it
> is best to keep the output operations seperate from the
> function calls. Some experinced will comment on this maybe...


In general, a single statement should have a single effect:
either control flow, assign to a single value, do input or
output, etc. In theory, anyway; not doing so has definite costs
in terms of readability and maintainability, but sometimes, the
alternatives have even higher cost. (This is not one of them,
however. Output is one of those things that tend to get
reworked a lot, since it is what the client sees most directly.
So in no case do you want to mix any of the program logic in
with it.)

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
  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 10h56.


Édité par : vBulletin® version 3.7.3
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 ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,15059 seconds with 17 queries