473,394 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Static variable in a member function

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
Feb 17 '06 #1
3 1555
Tomás wrote:
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.


-Tomás


Regards,
Ben
Feb 17 '06 #2
In message <H_******************@news.indigo.ie>, Tomás <NU**@NULL.NULL>
writes
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.

--
Richard Herring
Feb 17 '06 #3

"benben" <be******@yahoo.com.au> wrote in message
news:43***********************@news.optusnet.com.a u...
Tomas wrote:

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.


Once you change "ink" to "int", at least. :-)

-Howard

Feb 17 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

29
by: Alexander Mahr | last post by:
Dear Newsgroup, I'm somehow confused with the usage of the static keyword. I can see two function of the keyword static in conjunction with a data member of a class. 1. The data member...
7
by: lallous | last post by:
Hello, Why is the static variable inside a member function static for all instances and not static for the current instance of the object? -- Elias
1
by: Seb | last post by:
Is this efficient for a header file and a class? Does a 'static local' variable get created for each instance or include of the header file? Also, 'return _type().ToString();' does not seem to...
1
by: Bryan Parkoff | last post by:
I know how to write "Pointer to Function" inside struct or class without using static, but I have decided to add static to all functions inside struct or class because I want member functions to be...
5
by: John Goche | last post by:
Hello, I would like to know whethere there is a difference between a const variable and a static const variable inside a class. After all, if a variable is const in a class, the compiler can...
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
9
by: Jess | last post by:
Hello, I was told that if I declare a static class constant like this: class A{ static const int x = 10; }; then the above statement is a declaration rather than a definition. As I've...
1
by: rhd | last post by:
Hi, - C++ class with a private member function. - Private member function declares a static int locally with an initial value of 0 . - A single instance of the class is created. - The object...
17
by: Juha Nieminen | last post by:
As we know, the keyword "inline" is a bit misleading because its meaning has changed in practice. In most modern compilers it has completely lost its meaning of "a hint for the compiler to inline...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.