Static variable & static method

Discussion in 'C++' started by Stefan Istrate, Jun 6, 2008.

  1. 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
     
    Stefan Istrate, Jun 6, 2008
    #1
    1. Advertising

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

    --
    Erik Wikström
     
    Erik Wikström, Jun 6, 2008
    #2
    1. Advertising

  3. On Jun 6, 1:34 pm, Erik Wikström <> 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
     
    Stefan Istrate, Jun 6, 2008
    #3
  4. Stefan Istrate

    Markus Moll Guest

    Hi

    Stefan Istrate wrote:

    > On Jun 6, 1:34 pm, Erik Wikström <> 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
     
    Markus Moll, Jun 6, 2008
    #4
  5. Stefan Istrate a écrit :
    > On Jun 6, 1:34 pm, Erik Wikström <> 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
     
    Michael DOUBEZ, Jun 6, 2008
    #5
  6. So I should never modify a variable used more than once during
    evaluating an expression.
     
    Stefan Istrate, Jun 6, 2008
    #6
  7. 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
     
    Michael DOUBEZ, Jun 6, 2008
    #7
  8. Stefan Istrate

    utab Guest

    On 6 jun, 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 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...
     
    utab, Jun 6, 2008
    #8
  9. Stefan Istrate

    James Kanze Guest

    On Jun 6, 7:55 pm, utab <> wrote:
    > On 6 jun, 12:20, Stefan Istrate <> 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 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:
    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
     
    James Kanze, Jun 7, 2008
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Patrick Hoffmann
    Replies:
    3
    Views:
    2,852
    Christian Janßen
    Aug 8, 2003
  2. Marcin Vorbrodt

    Global static variable vs static method

    Marcin Vorbrodt, Sep 5, 2003, in forum: C++
    Replies:
    3
    Views:
    5,435
    Denis Perelyubskiy
    Sep 5, 2003
  3. mfglinux
    Replies:
    11
    Views:
    726
    Roberto Bonvallet
    Sep 12, 2007
  4. ZelluX
    Replies:
    24
    Views:
    914
    Hendrik Maryns
    Nov 19, 2007
  5. Aakash
    Replies:
    3
    Views:
    3,956
    Roedy Green
    Jan 19, 2008
Loading...

Share This Page