C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

statics in member functions

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Serge Skorokhodov (216716
Guest





PostPosted: Wed Jul 27, 2005 8:58 am    Post subject: statics in member functions Reply with 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?
Back to top
Greg
Guest





PostPosted: Wed Jul 27, 2005 9:20 am    Post subject: Re: statics in member functions Reply with quote





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





PostPosted: Wed Jul 27, 2005 11:01 am    Post subject: Re: statics in member functions Reply with quote



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





PostPosted: Wed Jul 27, 2005 11:59 am    Post subject: Re: statics in member functions Reply with quote



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





PostPosted: Wed Jul 27, 2005 7:16 pm    Post subject: Re: statics in member functions Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.