tags:

views:

89

answers:

2
class MyClass
{
public:
 void method2()
 {
  static int i;
  ...
 }
};

Will every instance of MyClass share one value i, or will each instance have its own copy?

+7  A: 

Every instance of MyClass will share one value i.

KennyTM
+7  A: 

static, here, operates as in any regular function.

Which means that i is static within MyClass::method2, so there is one and only one instance of it.

Having one instance of a variable per object is what instance variables are for.

RaphaelSP