static class local variable in memeber function

Discussion in 'C++' started by john, Aug 12, 2005.

  1. john

    john Guest

    By doing:

    void MyClass::MyFunction()
    {
    static int myvar;
    ....
    }

    We can defined a function local variable 'myvar' that keeps its value
    from call to call (the point is that the variable can not be easily
    accessed from outside the function, *that* is what we want to achieve)

    But this variable is global to *all* instance of the MyClass class. Is
    there an artifact that allow a similar definition but for a variable
    that is instance-based.

    ?
     
    john, Aug 12, 2005
    #1
    1. Advertising

  2. john

    Rolf Magnus Guest

    john wrote:

    > By doing:
    >
    > void MyClass::MyFunction()
    > {
    > static int myvar;
    > ...
    > }
    >
    > We can defined a function local variable 'myvar' that keeps its value
    > from call to call (the point is that the variable can not be easily
    > accessed from outside the function, *that* is what we want to achieve)
    >
    > But this variable is global to *all* instance of the MyClass class. Is
    > there an artifact that allow a similar definition but for a variable
    > that is instance-based.
    >
    > ?


    Make it a member variable.
     
    Rolf Magnus, Aug 12, 2005
    #2
    1. Advertising

  3. john

    msalters Guest

    john schreef:

    > By doing:
    >
    > void MyClass::MyFunction()
    > {
    > static int myvar;
    > ...
    > }
    >
    > We can defined a function local variable 'myvar' that keeps its value
    > from call to call (the point is that the variable can not be easily
    > accessed from outside the function, *that* is what we want to achieve)
    >
    > But this variable is global to *all* instance of the MyClass class. Is
    > there an artifact that allow a similar definition but for a variable
    > that is instance-based.


    std::map<MyClass*, int> myvars;
    myvars[this] = ...

    HTH,
    Michiel Salters
     
    msalters, Aug 12, 2005
    #3
  4. * john:
    > By doing:
    >
    > void MyClass::MyFunction()
    > {
    > static int myvar;
    > ...
    > }
    >
    > We can defined a function local variable 'myvar' that keeps its value
    > from call to call (the point is that the variable can not be easily
    > accessed from outside the function, *that* is what we want to achieve)
    >
    > But this variable is global to *all* instance of the MyClass class. Is
    > there an artifact that allow a similar definition but for a variable
    > that is instance-based.
    >
    > ?


    Not really.

    But consider that if 'myvar' is to be restricted to a _single_ function,
    then it is a local variable.

    And if it is to be restricted to a _set_ of member functions, then the
    functionality it partakes in is also restricted to that set, so the set
    forms its own class. I.e. you can make that set of member functions, plus
    variable, a separate class that 'MyClass' inherits from. Or that 'MyClass'
    contains an instance variable of.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
     
    Alf P. Steinbach, Aug 12, 2005
    #4
  5. john

    john Guest

    Rolf Magnus a écrit :
    > john wrote:
    >
    >
    >>By doing:
    >>
    >>void MyClass::MyFunction()
    >>{
    >>static int myvar;
    >>...
    >>}
    >>
    >>We can defined a function local variable 'myvar' that keeps its value
    >>from call to call (the point is that the variable can not be easily
    >>accessed from outside the function, *that* is what we want to achieve)
    >>
    >>But this variable is global to *all* instance of the MyClass class. Is
    >>there an artifact that allow a similar definition but for a variable
    >>that is instance-based.
    >>
    >>?

    >
    >
    > Make it a member variable.
    >

    Well and it can be accedeed from outside the function. you did not
    understand the point.
     
    john, Aug 12, 2005
    #5
  6. john wrote:
    > Rolf Magnus a écrit :
    >
    >> john wrote:
    >>
    >>
    >>> By doing:
    >>>
    >>> void MyClass::MyFunction()
    >>> {
    >>> static int myvar;
    >>> ...
    >>> }
    >>>
    >>> We can defined a function local variable 'myvar' that keeps its value
    >>> from call to call (the point is that the variable can not be easily
    >>> accessed from outside the function, *that* is what we want to achieve)
    >>>
    >>> But this variable is global to *all* instance of the MyClass class. Is
    >>> there an artifact that allow a similar definition but for a variable
    >>> that is instance-based.
    >>>
    >>> ?

    >>
    >>
    >>
    >> Make it a member variable.
    >>

    > Well and it can be accedeed from outside the function.


    So what? Make it private, so only members of this class can use it.

    > you did not
    > understand the point.


    What problem are you trying to solve and why would a private non-static
    member variable not work?

    V
     
    Victor Bazarov, Aug 12, 2005
    #6
    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. Ryan
    Replies:
    0
    Views:
    325
  3. josh
    Replies:
    5
    Views:
    303
    Dave Rahardja
    Feb 13, 2007
  4. Replies:
    2
    Views:
    498
    James Kanze
    Feb 4, 2009
  5. puzzlecracker
    Replies:
    3
    Views:
    812
    Andrey Tarasevich
    Mar 11, 2009
Loading...

Share This Page