Static variable in a member function

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
 
B

benben

Tomás said:
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?
Yes.

Or is there a separate
"k" variable for each object?
No.

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.
Correct.


So what's the best way to achieve this model of having a static variable
for a function, but keeping it separate between objects?

Use a member variable.
Is a member variable the way to go? As in:
Correct


class Monkey
{
ink k;

Monkey() : k(4) {}

int cow()
{
++k;

return k;
}
};

Yes, the above approach solves your problem.

Regards,
Ben
 
R

Richard Herring

Tomás <[email protected]> said:
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?

Yes. There's only one instance of the function for the entire class, and
so only one k. It's no different in this respect from having a
non-member function which takes a pointer or reference to a Monkey as an
argument.
Or is there a separate
"k" variable for each object? I'd test this myself but I haven't got a
compiler handy.

Nope. Think about the convolutions the compiler would have to go
through, to keep track of every static variable in every function that
Monkey might possess, and somehow store a separate instance of each of
them in each instance of the class.
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?

Think about the couplings here. Is the variable really a logical part of
the function? If you need a separate one for each object, surely it's
logically a part of the object, not the function?
Is a member variable the way to go? As in:

class Monkey
{
ink k;

Monkey() : k(4) {}

int cow()
{
++k;

return k;
}
};

Exactly. Anything that needs one instance per object probably ought to
be a (non-static) member of the class.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top