|
|
|
#1 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#2 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#3 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#4 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#5 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#6 |
|
Messages: n/a
Hébergeur: |
So I should never modify a variable used more than once during
evaluating an expression. |
|
|
|
#7 |
|
Messages: n/a
Hébergeur: |
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 |
|
|
|
#8 |
|
Messages: n/a
Hébergeur: |
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... |
|
|
|
#9 |
|
Messages: n/a
Hébergeur: |
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 |
|
![]() |
| Outils de la discussion | |
|
|