They are static vars in the method body, shared by all instances.

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

Will each instance of MyClass have the same i value, or will each instance have its own copy?

+6
c ++
source share
2 answers

static acts like in any regular function.

This means that i static inside MyClass::method2 , so there is only one instance.

Having one instance of a variable for each object is instance variables.

+7
source share

Each instance of MyClass will have one i value.

+7
source share

All Articles