By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,920 Members | 1,226 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,920 IT Pros & Developers. It's quick & easy.

statics in member functions

P: n/a
Hi,

It may be quite a silly question but I'm a bit in a doubt;)

Say:

class A
{
void f();
....
};

void A::f()
{
static int i = 1;
....
i = something_complex_calculation_with_unpredictable_r esult();
}

Some other place:

void ff()
{
....
A* pa = new A;
....
pa->f();
....
delete pa;
....
}

Q: The value of i persists between calls of ff(), doesn't it?
Does the Standard require it?
Jul 27 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a


Serge Skorokhodov (216716244) wrote:
Hi,

It may be quite a silly question but I'm a bit in a doubt;)

Say:

class A
{
void f();
...
};

void A::f()
{
static int i = 1;
...
i = something_complex_calculation_with_unpredictable_r esult();
}

Some other place:

void ff()
{
...
A* pa = new A;
...
pa->f();
...
delete pa;
...
}

Q: The value of i persists between calls of ff(), doesn't it?
Does the Standard require it?


Yes. The fact that i is declared in a class method instead of a global
function changes nothing about its behavior.

Greg

Jul 27 '05 #2

P: n/a
Ram
> Yes. The fact that i is declared in a class method instead of a global
function changes nothing about its behavior.


A further question on this. Local static variables inside class member
functions are instantiated per object or per class? g++ (2.96)
instantiates it per class i.e. if I say

A a1, a2;
a1.ff();
a2.ff();

I found that the value persists between the two calls. Is this also
required by the standard?

Ramashish

Jul 27 '05 #3

P: n/a


Ram wrote:
Yes. The fact that i is declared in a class method instead of a global
function changes nothing about its behavior.


A further question on this. Local static variables inside class member
functions are instantiated per object or per class? g++ (2.96)
instantiates it per class i.e. if I say

A a1, a2;
a1.ff();
a2.ff();

I found that the value persists between the two calls. Is this also
required by the standard?

Ramashish


they are instantiated per class (first time the method is call,
regardless of the instantiated number of objects of that type). If you
want it to be instantiated on a per object basis, make the variable a
class level, member variable not a static variable inside the method.

class A
{
private:
int m_var; // instantiated per object

public:
A():m_var(0){}

void f()
{
static int x = some_function_returning_an_int(); // instantiated
per class
}
};
/dan

Jul 27 '05 #4

P: n/a
On Wed, 27 Jul 2005 04:01:01 -0700, Ram wrote:
Yes. The fact that i is declared in a class method instead of a global
function changes nothing about its behavior.


A further question on this. Local static variables inside class member
functions are instantiated per object or per class? g++ (2.96)
instantiates it per class i.e. if I say

A a1, a2;
a1.ff();
a2.ff();

I found that the value persists between the two calls. Is this also
required by the standard?


Neither. The variable is instantiated the first time the function is
called, and it persists through subsequent calls to that function. It has
nothing to do with objects or classes.

- Jay
Jul 27 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.