T
Tomás
class Monkey
{
int cow()
{
static int k = 4;
++k;
return k;
}
};
Let's say we create three objects of "Monkey":
Monkey a;
Monkey b;
Monkey c;
Is "k" the same variable for all three objects? Or is there a separate
"k" variable for each object? I'd test this myself but I haven't got a
compiler handy.
My gut feeling tells me that there is only the one "k" variable, and it's
used by all objects.
So what's the best way to achieve this model of having a static variable
for a function, but keeping it separate between objects?
Is a member variable the way to go? As in:
class Monkey
{
ink k;
Monkey() : k(4) {}
int cow()
{
++k;
return k;
}
};
-Tomás
{
int cow()
{
static int k = 4;
++k;
return k;
}
};
Let's say we create three objects of "Monkey":
Monkey a;
Monkey b;
Monkey c;
Is "k" the same variable for all three objects? Or is there a separate
"k" variable for each object? I'd test this myself but I haven't got a
compiler handy.
My gut feeling tells me that there is only the one "k" variable, and it's
used by all objects.
So what's the best way to achieve this model of having a static variable
for a function, but keeping it separate between objects?
Is a member variable the way to go? As in:
class Monkey
{
ink k;
Monkey() : k(4) {}
int cow()
{
++k;
return k;
}
};
-Tomás