Does a static variable in a class's member fn always remain static?

Discussion in 'C++' started by Sam, Jan 13, 2004.

  1. Sam

    Sam Guest

    I'm thinking about this question. I've a class A and its member
    function fn. In fn there's a static variable s.

    If I create an object by
    A a;

    When I call the function a.fn, s would remain static throught out all
    the callings of a.fn, in other words it would be affected by all the
    callings of a.fn.

    Now the question is, if I've created other objects by
    A b,c,d;

    When I call the function b.fn, c.fn, d.fn, will they also affect the
    value of the static s?
    Sam, Jan 13, 2004
    #1
    1. Advertising

  2. Sam

    John Carson Guest

    "Sam" <> wrote in message
    news:
    > I'm thinking about this question. I've a class A and its member
    > function fn. In fn there's a static variable s.
    >
    > If I create an object by
    > A a;
    >
    > When I call the function a.fn, s would remain static throught out all
    > the callings of a.fn, in other words it would be affected by all the
    > callings of a.fn.
    >
    > Now the question is, if I've created other objects by
    > A b,c,d;
    >
    > When I call the function b.fn, c.fn, d.fn, will they also affect the
    > value of the static s?



    Why don't you try it and see?

    The answer is yes. Individual objects have their own copies of a class's
    data members, but there is only one copy of each member function and it is
    shared by all objects of the class. Thus a.fn(), b.fn(), c.fn(), d.fn() etc.
    are all calling the same function containing the same static variable.


    --
    John Carson
    1. To reply to email address, remove donald
    2. Don't reply to email address (post here instead)
    John Carson, Jan 13, 2004
    #2
    1. Advertising

  3. Sam

    Derek Guest

    "Sam" wrote:
    > I'm thinking about this question. I've a class A and its
    > member function fn. In fn there's a static variable s.
    >
    > If I create an object by A a;
    >
    > When I call the function a.fn, s would remain static
    > throught out all the callings of a.fn, in other words it
    > would be affected by all the callings of a.fn.
    >
    > Now the question is, if I've created other objects by A
    > b,c,d;
    >
    > When I call the function b.fn, c.fn, d.fn, will they also
    > affect the value of the static s?


    Yes.
    Derek, Jan 13, 2004
    #3
  4. Sam

    Sumit Rajan Guest

    "Sam" <> wrote in message
    news:...
    > I'm thinking about this question. I've a class A and its member
    > function fn. In fn there's a static variable s.
    >
    > If I create an object by
    > A a;
    >
    > When I call the function a.fn, s would remain static throught out all
    > the callings of a.fn, in other words it would be affected by all the
    > callings of a.fn.
    >
    > Now the question is, if I've created other objects by
    > A b,c,d;
    >
    > When I call the function b.fn, c.fn, d.fn, will they also affect the
    > value of the static s?


    Why don't you try it out?

    #include <iostream>

    class X {
    int i;
    public:
    X(){i=0;};
    int fn();
    };

    int X::fn()
    {
    static int s = 0;
    return ++s;
    }

    int main()
    {

    X a;
    std::cout << a.fn() << '\n';
    X b;
    std::cout << b.fn() << '\n';
    std::cout << a.fn() << '\n';
    }


    Does that answer your question?
    The static variable, s, retains its value between calls.

    Sumit.
    Sumit Rajan, Jan 13, 2004
    #4
  5. [Snipped]

    >
    > When I call the function b.fn, c.fn, d.fn, will they also affect the
    > value of the static s?


    Absolutely yes (A simple test program would have given you the answer.)

    --The Directive
    The Directive, Jan 13, 2004
    #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. Siemel Naran
    Replies:
    4
    Views:
    800
    Micah Cowan
    Jan 12, 2005
  2. Bob
    Replies:
    2
    Views:
    410
  3. Dan
    Replies:
    9
    Views:
    426
  4. Yash
    Replies:
    1
    Views:
    427
    bruce barker
    Dec 30, 2008
  5. BigDadyWeaver
    Replies:
    6
    Views:
    218
    BigDaddyWeaver
    Feb 3, 2004
Loading...

Share This Page