470,870 Members | 1,431 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,870 developers. It's quick & easy.

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 help me understand this?
Thank you,
Stefan Istrate
Jun 27 '08 #1
3 1330
So I should never modify a variable used more than once during
evaluating an expression.
Jun 27 '08 #2
On 6 jun, 12:20, Stefan Istrate <stefan.istr...@gmail.comwrote:
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...
Jun 27 '08 #3
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
Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

18 posts views Thread by Frank Rizzo | last post: by
15 posts views Thread by rwf_20 | last post: by
44 posts views Thread by John A. Bailo | last post: by
11 posts views Thread by toton | last post: by
37 posts views Thread by minkoo.seo | last post: by
reply views Thread by Joe Strout | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.