Static variables in member functions

2019-01-03 12:19发布

Can someone please explain how static variables in member functions work in C++.

Given the following class:

class A {
   void foo() {
      static int i;
      i++;
   }
}

If I declare multiple instances of A, does calling foo() on one instance increment the static variable i on all instances? Or only the one it was called on?

I assumed that each instance would have its own copy of i, but stepping through some code I have seems to indicate otherwise.

标签: c++ static scope
3条回答
Anthone
2楼-- · 2019-01-03 12:31

Since class A is a non-template class and A::foo() is a non-template function. There will be only one copy of static int i inside the program.

Any instance of A object will affect the same i and lifetime of i will remain through out the program. To add an example:

A o1, o2, o3;
o1.foo(); // i = 1
o2.foo(); // i = 2
o3.foo(); // i = 3
o1.foo(); // i = 4
查看更多
【Aperson】
3楼-- · 2019-01-03 12:49

The keyword static unfortunately has a few different unrelated meanings in C++

  1. When used for data members it means that the data is allocated in the class and not in instances.

  2. When used for data inside a function it means that the data is allocated statically, initialized the first time the block is entered and lasts until the program quits. Also the variable is visible only inside the function. This special feature of local statics is often used to implement lazy construction of singletons.

  3. When used at a compilation unit level (module) it means that the variable is like a global (i.e. allocated and initialized before main is run and destroyed after main exits) but that the variable will not be accessible or visible in other compilation units.

I added some emphasis on the part that is most important for each use. Use (3) is somewhat discouraged in favor of unnamed namespaces that also allows for un-exported class declarations.

In your code the static keyword is used with the meaning number 2 and has nothing to do with classes or instances... it's a variable of the function and there will be only one copy of it.

As correctly iammilind said however there could have been multiple instances of that variable if the function was a template function (because in that case indeed the function itself can be present in many different copies in the program). Even in that case of course classes and instances are irrelevant... see following example:

#include <stdio.h>

template<int num>
void bar()
{
    static int baz;
    printf("bar<%i>::baz = %i\n", num, baz++);
}

int main()
{
    bar<1>(); // Output will be 0
    bar<2>(); // Output will be 0
    bar<3>(); // Output will be 0
    bar<1>(); // Output will be 1
    bar<2>(); // Output will be 1
    bar<3>(); // Output will be 1
    bar<1>(); // Output will be 2
    bar<2>(); // Output will be 2
    bar<3>(); // Output will be 2
    return 0;
}
查看更多
【Aperson】
4楼-- · 2019-01-03 12:51

Simplified answer:

Static variables, regardless whether they are members of a (non-templated) class or a (non-templated) function, behave - technically - like a global label which scope is limited to the class or function.

查看更多
登录 后发表回答