473,244 Members | 1,457 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,244 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 2151
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

29
by: Alexander Mahr | last post by:
Dear Newsgroup, I'm somehow confused with the usage of the static keyword. I can see two function of the keyword static in conjunction with a data member of a class. 1. The data member...
1
by: Seb | last post by:
Is this efficient for a header file and a class? Does a 'static local' variable get created for each instance or include of the header file? Also, 'return _type().ToString();' does not seem to...
2
by: cppaddict | last post by:
Hi, I have the following line inside one of my class member functions: static bool tested = false; if (!tested) { MessageBox(NULL, "Hi", "Testing", MB_OK | MB_ICONEXCLAMATION); tested =...
1
by: Bryan Parkoff | last post by:
I know how to write "Pointer to Function" inside struct or class without using static, but I have decided to add static to all functions inside struct or class because I want member functions to be...
2
by: katekukku | last post by:
HI, Could anyone please tell me what are static variables and what exactly are there features. I am a little bit confused. Thank You
5
by: John Goche | last post by:
Hello, I would like to know whethere there is a difference between a const variable and a static const variable inside a class. After all, if a variable is const in a class, the compiler can...
9
by: Jess | last post by:
Hello, I was told that if I declare a static class constant like this: class A{ static const int x = 10; }; then the above statement is a declaration rather than a definition. As I've...
10
by: Pramod | last post by:
Hello to all of you, I want to know that what's the use to create static object. Thanks You Pramod Sahgal
3
by: Bryan Parkoff | last post by:
The local variables and local functions are inside class body. You define a variable to the class "Reg reg;" in the main function. The reg variable has a pointer. The pointer gives memory...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
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
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...

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.