473,325 Members | 2,608 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,325 software developers and data experts.

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_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
4 1193


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


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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Wenjie | last post by:
Hello, I read someone posted assertions that even the (public) member function is not static, there are probably only one copy of the code in the executable. Then except the...
2
by: Thomas Matthews | last post by:
Hi, I have a hierarchy of classes and would like to create an array of pointers to member functions that could also contain pointers to the parent member functions as well. What is the syntax...
5
by: Stuart MacMartin | last post by:
I have a problem with static lifetime (order of destruction of statics within different cpp files). I have a workaround that happens to work in my case. I'd like to know if this is luck or...
10
by: hufaunder | last post by:
I have an interface where I would like to have some static functions. When I add "static" to any of the interface functions I get the following error: "The modifier 'static' is not valid for this...
20
by: Aek | last post by:
We recently moved our large codebase over from VS7 to 8 and found that we now get access violations in atexit calls at shutdown when debugging the application in VS2005. This occurs in static...
13
by: JohnQ | last post by:
The implementation of classes with virtual functions is conceptually easy to understand: they use vtables. Which begs the question about POD structs: how are they associated with their member...
3
by: DR | last post by:
I heard there is some trick to referencing statics in C# CLR stored procedure without having to mark the assembly as unsafe. Does anyone know this? This is usefull as the case of needing a little...
3
by: =?Utf-8?B?TWFyaw==?= | last post by:
Hi... At least by the group title, this seems like a question for dotnet.framework.aspnet.caching but that group seems pretty slow. I'm trying to sort things out with a co-worker. We've got...
7
by: Immortal Nephi | last post by:
My project grows large when I put too many member functions into one class. The header file and source code file will have approximately 50,000 lines when one class contains thousand member...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.