Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do static local variables in a class persist between objects?

class MyClass
{
  static int staticInt;

  void instanceMethod( int param )
  {
    static int parameter = param;
  }
}

Clearly staticInt is shared between all instance of MyClass. But can different instances of MyClass have different values of parameter the static local variable within instaceMethod?

Update

What about between program executions? Certainly they could be different in multiple program instances? What defines "scope" there - the execution unit? The c++ runtime?

Update

Thanks - this helped me squash a critical bug. Wish I could accept them all, but I'm going with the first answer with no other criteria.

like image 245
mindless.panda Avatar asked Jun 10 '10 17:06

mindless.panda


1 Answers

There is exactly one instance of parameter.

If you want an instance of parameter for each instance of the class, use a nonstatic member variable.

like image 163
James McNellis Avatar answered Sep 18 '22 23:09

James McNellis