Given the class:
class A {
Public:
void foo() {
static int i;
i++;
}
};
How would you change it to prevent i
from changing between instances following this example:
A o1, o2, o3;
o1.foo(); // i = 1
o2.foo(); // i = 1
o3.foo(); // i = 1
o1.foo(); // i = 2
i.e. allocate memory for i
on every instance.
EDIT:
Yes, you can add i
as an instance variable, but what if you need these counters in various (independent) functions ? I am looking to limit the scope of the variable only to the function ("in member functions"). It would seem awkward to add variables such as i
, c
, counter
, counter_2
to the class if you need various counters, would it not ?