| View previous topic :: View next topic |
| Author |
Message |
Serge Skorokhodov (216716 Guest
|
Posted: Wed Jul 27, 2005 8:58 am Post subject: statics in member functions |
|
|
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_result();
}
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?
|
|
| Back to top |
|
 |
Greg Guest
|
Posted: Wed Jul 27, 2005 9:20 am Post subject: Re: statics in member functions |
|
|
Serge Skorokhodov (216716244) wrote:
| Quote: | 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_result();
}
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
|
|
| Back to top |
|
 |
Ram Guest
|
Posted: Wed Jul 27, 2005 11:01 am Post subject: Re: statics in member functions |
|
|
| Quote: | 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
|
|
| Back to top |
|
 |
Dan Cernat Guest
|
Posted: Wed Jul 27, 2005 11:59 am Post subject: Re: statics in member functions |
|
|
Ram wrote:
| Quote: | 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
|
|
| Back to top |
|
 |
Jay Nabonne Guest
|
Posted: Wed Jul 27, 2005 7:16 pm Post subject: Re: statics in member functions |
|
|
On Wed, 27 Jul 2005 04:01:01 -0700, Ram wrote:
| Quote: | 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
|
|
| Back to top |
|
 |
|