A static variable in a member function is a class level variable, meaning that all class instances access the same variable.
class foo{
int doingSomething(){
static int count =0;
static int someValue=0
count++;
if(count%10 == 0){
someValue *= counter;
}
someValue += counter;
return someValue * 2;
}
}
I want a way to count the number of times count() was called in each instance of foo separately, i can do this :
class foo{
int count;
int someValue;
int doingSomething(){
count++;
if(count%10 == 0){
someValue *= counter;
}
someValue += counter;
return someValue * 2;
}
but my problem with this approach is that count and somevalue is only used and accessed in foo by doingSomething(), so there is no reason for it to be a member variable, since it opens the door to it modified and used by other class member function, and i want to prevent that.
is there a solution that i am missing ?
EDIT 1:
The idea is to count the number of time that doingSomething() was called in each instance of Foo, and this value will be used only by 'doingSomething()' to compute other values, which will be different for each instance.
Why?
doingSomething() compute a int someVariable the first time is called, then stores it for later use, this stored value is used by doingSomthing() 10 times with every call, every 11calls int someVariable is recalculates and this new value is used...process repeated indefinitely.