Static variable & static method

S

Stefan Istrate

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 help me understand this?
Thank you,
Stefan Istrate
 
E

Erik Wikström

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 help 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().
 
S

Stefan Istrate

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.

Stefan Istrate
 
M

Markus Moll

Hi

Stefan said:
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
 
M

Michael DOUBEZ

Stefan Istrate a écrit :
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.
 
S

Stefan Istrate

So I should never modify a variable used more than once during
evaluating an expression.
 
U

utab

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 help 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...
 
J

James Kanze

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.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top