Info on Static Variables Needed by SHIKHAR

K

katekukku

HI,
Could anyone please tell me what are static variables and what exactly
are there features.
I am a little bit confused.

Thank You
 
M

Mike Wahler

HI,
Could anyone please tell me what are static variables and what exactly
are there features.
I am a little bit confused.

Object names qualified with the keyword 'static'
are give 'static storage duration', that is, they
have a lifetime equal to that of the program's
execution. 'static' objects are also default-
initialized if no explicit initial value is
given.

Which C++ book(s) are you reading which don't
explain this?

-Mike
 
B

Bob Hairgrove

HI,
Could anyone please tell me what are static variables and what exactly
are there features.
I am a little bit confused.

There are static member variables, local static variables inside a
function definition, and global static variables at file scope.

In a nutshell, storage for static member variables is allocated
independently of any object of the class of which they are a member.
They must be initialized outside of the class definition (i.e. at
global scope).

Inside the body of a function definition, storage for a static
variable is allocated before the program runs. The value of such a
variable remains persistent beyond the scope of the function being
called. The initialization of such function static variables is done
before the function is called the first time -- sometimes well before,
but sometimes only at the last moment. I believe this is
implementation defined (please someone correct me if I'm mistaken,
don't have time right now to look it up...).

Variables declared globally outside a class or function body with the
keyword "static" are only visible at file scope in the translation
unit in which they are defined. IOW, you can have multiple variables
with the same name if they are all declared with the keyword "static"
and there is only one per translation unit.
 

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
474,206
Messages
2,571,069
Members
47,675
Latest member
KevinStepp

Latest Threads

Top