static variable inside a member function

Discussion in 'C++' started by lallous, Jan 14, 2004.

  1. lallous

    lallous Guest

    Hello,

    Why is the static variable inside a member function static for all instances
    and not static for the current instance of the object?

    --
    Elias
     
    lallous, Jan 14, 2004
    #1
    1. Advertising

  2. lallous wrote:

    > Hello,
    >
    > Why is the static variable inside a member function static for all instances
    > and not static for the current instance of the object?
    >
    > --
    > Elias
    >
    >


    Because that would require storage for each instance, instead of the
    single instance as defined by the keyword static... might as well be a
    member variable (just a guess).

    Although, if it is a member variable, the scope is available to all
    member functions, and not constrained to the one in question.

    Brian
     
    Brian Genisio, Jan 14, 2004
    #2
    1. Advertising

  3. lallous wrote:

    > Hello,
    >
    > Why is the static variable inside a member function static for all instances
    > and not static for the current instance of the object?
    >


    It would not comply with the general static variable concept.

    All variables marked with the keyword static indicate variables which
    lifetime is as long as the lifetime of the program. Hence they exist
    even when no object of that type was created only you cannot access them
    outside of their visibility scope.

    So no object can have its own static variable copy and all such
    variables are shared by all entities that can access given function. It
    is identical as in the case of static data member.

    Regards,
    Janusz

    Only are not accessible in the case when they are object functions

    How would it differ then from an object data member?
     
    Janusz Szpilewski, Jan 14, 2004
    #3
  4. lallous wrote:

    > Hello,
    >
    > Why is the static variable inside a member function static for all

    instances
    > and not static for the current instance of the object?
    >


    It would not comply with the general static variable concept.

    All variables marked with the keyword static indicate variables which
    lifetime is as long as the lifetime of the program. Hence they exist
    even when no object of that type was created only you cannot access them
    outside of their visibility scope.

    So no object can have its own static variable copy and all such
    variables are shared by all entities that can access given function. It
    is identical as in the case of static data member.

    Regards,
    Janusz
     
    Janusz Szpilewski, Jan 14, 2004
    #4
  5. lallous wrote:
    >
    > Hello,
    >
    > Why is the static variable inside a member function static for all instances
    > and not static for the current instance of the object?


    Because if you want such a thing you use a simple class member variable.


    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Jan 14, 2004
    #5
  6. "Janusz Szpilewski" <> schrieb im Newsbeitrag
    news:bu3h9i$dp1$...
    > lallous wrote:
    >
    > All variables marked with the keyword static indicate variables which
    > lifetime is as long as the lifetime of the program. Hence they exist
    > even when no object of that type was created only you cannot access them
    > outside of their visibility scope.

    Not so for a statics inside (member) functions. statics in (member)
    functions
    begin their life when program flow passes their definition for the first
    time. So
    they won't exist, when the definition is never passed.

    Bye

    Norbert
     
    Norbert Riedlin, Jan 14, 2004
    #6
  7. Norbert Riedlin wrote:

    > Not so for a statics inside (member) functions. statics in (member)
    > functions
    > begin their life when program flow passes their definition for the first
    > time. So
    > they won't exist, when the definition is never passed.
    >


    True as it concerns static data initialization. However memory for such
    data, what means its identity, is allocated in the global (static) data
    block as soon as the program starts and lasts for the duration of the
    program (C++ std 3.7.1/1). Additionally it is initialized before it gets
    used. So with some simplification we may say that there is always
    existing one unique copy of a given static variable.

    Regards,
    Janusz
     
    Janusz Szpilewski, Jan 14, 2004
    #7
  8. "Janusz Szpilewski" <> schrieb im Newsbeitrag
    news:bu4b5s$n42$...
    > Norbert Riedlin wrote:
    >
    > > Not so for a statics inside (member) functions. statics in (member)
    > > functions
    > > begin their life when program flow passes their definition for the first
    > > time. So
    > > they won't exist, when the definition is never passed.
    > >

    >
    > True as it concerns static data initialization. However memory for such
    > data, what means its identity, is allocated in the global (static) data
    > block as soon as the program starts and lasts for the duration of the
    > program (C++ std 3.7.1/1). Additionally it is initialized before it gets
    > used. So with some simplification we may say that there is always
    > existing one unique copy of a given static variable.


    Ok, but you talked about the lifetime of the variable, which I took as a
    synonym for the lifetime of the object. And the lifetime of an object
    clearly starts, when its constructor has run. But you are right, the
    raw memory is reserved at loadtime.

    Bye

    Norbert
     
    Norbert Riedlin, Jan 15, 2004
    #8
    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. slide_o_mix
    Replies:
    0
    Views:
    425
    slide_o_mix
    Oct 15, 2003
  2. Alex
    Replies:
    0
    Views:
    396
  3. 0to60
    Replies:
    4
    Views:
    520
    jeffc
    Nov 21, 2003
  4. aling
    Replies:
    6
    Views:
    486
    Xiaobin.Huang
    Oct 30, 2005
  5. dolphin
    Replies:
    3
    Views:
    1,354
    Pete Becker
    Dec 5, 2007
Loading...

Share This Page