471,585 Members | 1,227 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,585 software developers and data experts.

static variable inside a member function

Hello,

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

--
Elias
Jul 22 '05 #1
7 2071
lallous wrote:
Hello,

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

--
Elias


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

Jul 22 '05 #2
lallous wrote:
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?

Jul 22 '05 #3
lallous wrote:
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

Jul 22 '05 #4
lallous wrote:

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.
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #5

"Janusz Szpilewski" <sz******@poczta.onet.pl> schrieb im Newsbeitrag
news:bu**********@news.onet.pl...
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
Jul 22 '05 #6
Norbert Riedlin wrote:
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
Jul 22 '05 #7
"Janusz Szpilewski" <sz******@poczta.onet.pl> schrieb im Newsbeitrag
news:bu**********@news.onet.pl...
Norbert Riedlin wrote:
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.


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
Jul 22 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

29 posts views Thread by Alexander Mahr | last post: by
2 posts views Thread by cppaddict | last post: by
1 post views Thread by Bryan Parkoff | last post: by
2 posts views Thread by katekukku | last post: by
5 posts views Thread by John Goche | last post: by
10 posts views Thread by Pramod | last post: by
3 posts views Thread by Bryan Parkoff | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by Anwar ali | last post: by

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.