statics in member functions

  • Thread starter Serge Skorokhodov (216716244)
  • Start date
S

Serge Skorokhodov (216716244)

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?
 
G

Greg

Serge said:
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
 
R

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
 
D

Dan Cernat

Ram said:
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
 
J

Jay Nabonne

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
 

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
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top