statics in member functions

Discussion in 'C++' started by Serge Skorokhodov (216716244), Jul 27, 2005.

  1. Hi,

    It may be quite a silly question but I'm a bit in a doubt;)

    Say:

    class A
    {
    void f();
    ....
    };

    void A::f()
    {
    static int i = 1;
    ....
    i = something_complex_calculation_with_unpredictable_result();
    }

    Some other place:

    void ff()
    {
    ....
    A* pa = new A;
    ....
    pa->f();
    ....
    delete pa;
    ....
    }

    Q: The value of i persists between calls of ff(), doesn't it?
    Does the Standard require it?
     
    Serge Skorokhodov (216716244), Jul 27, 2005
    #1
    1. Advertising

  2. Serge Skorokhodov (216716244)

    Greg Guest

    Serge Skorokhodov (216716244) wrote:
    > Hi,
    >
    > It may be quite a silly question but I'm a bit in a doubt;)
    >
    > Say:
    >
    > class A
    > {
    > void f();
    > ...
    > };
    >
    > void A::f()
    > {
    > static int i = 1;
    > ...
    > i = something_complex_calculation_with_unpredictable_result();
    > }
    >
    > Some other place:
    >
    > void ff()
    > {
    > ...
    > A* pa = new A;
    > ...
    > pa->f();
    > ...
    > delete pa;
    > ...
    > }
    >
    > Q: The value of i persists between calls of ff(), doesn't it?
    > Does the Standard require it?


    Yes. The fact that i is declared in a class method instead of a global
    function changes nothing about its behavior.

    Greg
     
    Greg, Jul 27, 2005
    #2
    1. Advertising

  3. Serge Skorokhodov (216716244)

    Ram Guest

    > Yes. The fact that i is declared in a class method instead of a global
    > function changes nothing about its behavior.


    A further question on this. Local static variables inside class member
    functions are instantiated per object or per class? g++ (2.96)
    instantiates it per class i.e. if I say

    A a1, a2;
    a1.ff();
    a2.ff();

    I found that the value persists between the two calls. Is this also
    required by the standard?

    Ramashish
     
    Ram, Jul 27, 2005
    #3
  4. Serge Skorokhodov (216716244)

    Dan Cernat Guest

    Ram wrote:
    > > Yes. The fact that i is declared in a class method instead of a global
    > > function changes nothing about its behavior.

    >
    > A further question on this. Local static variables inside class member
    > functions are instantiated per object or per class? g++ (2.96)
    > instantiates it per class i.e. if I say
    >
    > A a1, a2;
    > a1.ff();
    > a2.ff();
    >
    > I found that the value persists between the two calls. Is this also
    > required by the standard?
    >
    > Ramashish


    they are instantiated per class (first time the method is call,
    regardless of the instantiated number of objects of that type). If you
    want it to be instantiated on a per object basis, make the variable a
    class level, member variable not a static variable inside the method.

    class A
    {
    private:
    int m_var; // instantiated per object

    public:
    A():m_var(0){}

    void f()
    {
    static int x = some_function_returning_an_int(); // instantiated
    per class
    }
    };


    /dan
     
    Dan Cernat, Jul 27, 2005
    #4
  5. Serge Skorokhodov (216716244)

    Jay Nabonne Guest

    On Wed, 27 Jul 2005 04:01:01 -0700, Ram wrote:

    >> Yes. The fact that i is declared in a class method instead of a global
    >> function changes nothing about its behavior.

    >
    > A further question on this. Local static variables inside class member
    > functions are instantiated per object or per class? g++ (2.96)
    > instantiates it per class i.e. if I say
    >
    > A a1, a2;
    > a1.ff();
    > a2.ff();
    >
    > I found that the value persists between the two calls. Is this also
    > required by the standard?
    >


    Neither. The variable is instantiated the first time the function is
    called, and it persists through subsequent calls to that function. It has
    nothing to do with objects or classes.

    - Jay
     
    Jay Nabonne, Jul 27, 2005
    #5
    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. Jason

    Statics and connections

    Jason, Dec 6, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    1,752
    Jason
    Dec 6, 2004
  2. Roedy Green

    statics in inner classes

    Roedy Green, Jul 1, 2005, in forum: Java
    Replies:
    14
    Views:
    744
    Thomas G. Marshall
    Jul 12, 2005
  3. Stuart MacMartin
    Replies:
    5
    Views:
    565
    Victor Bazarov
    Jul 27, 2005
  4. Hicham Mouline
    Replies:
    0
    Views:
    435
    Hicham Mouline
    Apr 23, 2009
  5. Hicham Mouline
    Replies:
    1
    Views:
    421
    Michael DOUBEZ
    Apr 24, 2009
Loading...

Share This Page