On Jun 6, 7:55 pm, utab <umut.ta...@gmail.comwrote:
On 6 jun, 12:20, Stefan Istrate <stefan.istr...@gmail.comwrote:
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?
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:ja*********@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