473,385 Members | 1,642 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,385 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 2157
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
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.