static variable inside a member function

L

lallous

Hello,

Why is the static variable inside a member function static for all instances
and not static for the current instance of the object?
 
B

Brian Genisio

lallous said:
Hello,

Why is the static variable inside a member function static for all instances
and not static for the current instance of the object?

Because that would require storage for each instance, instead of the
single instance as defined by the keyword static... might as well be a
member variable (just a guess).

Although, if it is a member variable, the scope is available to all
member functions, and not constrained to the one in question.

Brian
 
J

Janusz Szpilewski

lallous said:
Hello,

Why is the static variable inside a member function static for all instances
and not static for the current instance of the object?

It would not comply with the general static variable concept.

All variables marked with the keyword static indicate variables which
lifetime is as long as the lifetime of the program. Hence they exist
even when no object of that type was created only you cannot access them
outside of their visibility scope.

So no object can have its own static variable copy and all such
variables are shared by all entities that can access given function. It
is identical as in the case of static data member.

Regards,
Janusz

Only are not accessible in the case when they are object functions

How would it differ then from an object data member?
 
J

Janusz Szpilewski

lallous said:
Hello,

Why is the static variable inside a member function static for all instances
and not static for the current instance of the object?

It would not comply with the general static variable concept.

All variables marked with the keyword static indicate variables which
lifetime is as long as the lifetime of the program. Hence they exist
even when no object of that type was created only you cannot access them
outside of their visibility scope.

So no object can have its own static variable copy and all such
variables are shared by all entities that can access given function. It
is identical as in the case of static data member.

Regards,
Janusz
 
K

Karl Heinz Buchegger

lallous said:
Hello,

Why is the static variable inside a member function static for all instances
and not static for the current instance of the object?

Because if you want such a thing you use a simple class member variable.
 
N

Norbert Riedlin

Janusz Szpilewski said:
lallous wrote:

All variables marked with the keyword static indicate variables which
lifetime is as long as the lifetime of the program. Hence they exist
even when no object of that type was created only you cannot access them
outside of their visibility scope.
Not so for a statics inside (member) functions. statics in (member)
functions
begin their life when program flow passes their definition for the first
time. So
they won't exist, when the definition is never passed.

Bye

Norbert
 
J

Janusz Szpilewski

Norbert said:
Not so for a statics inside (member) functions. statics in (member)
functions
begin their life when program flow passes their definition for the first
time. So
they won't exist, when the definition is never passed.

True as it concerns static data initialization. However memory for such
data, what means its identity, is allocated in the global (static) data
block as soon as the program starts and lasts for the duration of the
program (C++ std 3.7.1/1). Additionally it is initialized before it gets
used. So with some simplification we may say that there is always
existing one unique copy of a given static variable.

Regards,
Janusz
 
N

Norbert Riedlin

Janusz Szpilewski said:
True as it concerns static data initialization. However memory for such
data, what means its identity, is allocated in the global (static) data
block as soon as the program starts and lasts for the duration of the
program (C++ std 3.7.1/1). Additionally it is initialized before it gets
used. So with some simplification we may say that there is always
existing one unique copy of a given static variable.

Ok, but you talked about the lifetime of the variable, which I took as a
synonym for the lifetime of the object. And the lifetime of an object
clearly starts, when its constructor has run. But you are right, the
raw memory is reserved at loadtime.

Bye

Norbert
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top