Static variable inside a member function

Say you had:
1
2
3
4
5
6
7
8
9
10
11
12
13
class Foo{
   public:
//...
      void funky();
//...
   private:
//...
};

void Foo::funky(){
   static int counter(0);
//...
};


Would each instance of Foo create a new counter variable, or would it remain the same for all of them, i.e. baz.funky() would always use the same counter variable?

What if the class was a template?
Would each instance of Foo create a new counter variable, or would it remain the same for all of them


It would remain the same for all of them.


What if the class was a template?


Templates are not classes. A template creates a new class for each instantiation of it. So Foo<int> and Foo<char> would be 2 entirely different classes. Therefore Foo<int>::funky() and Foo<char>::funky would be 2 entirely different functions, and therefore would each have their own variable.
Okay thanks. Your response to my second question was actually exactly what I was thinking. Sorry about wording it incorrectly.
Nah, your wording was fine. I was just trying to illustrate why it works the way it does.
Topic archived. No new replies allowed.