Topic: Q: lifetime of static var in class member function
Author: clamage@eng.sun.com (Steve Clamage)
Date: 1999/10/14 Raw View
"Peter de Leeuw van Weenen" <pleforum@orion.nl> writes:
>What is the lifetime of a static variable defined in a member function of a
>class? Is it equal to the lifetime of the class, or is it equal to the
>lifetime of the program?
The lifetime of a static variable begins when the flow of control
first passes through its definition, and ends with the end of the
program.
Example:
class T { ... void f(int); };
class U { ... };
void T::f(int i)
{
if( i > 0) {
static U u(args);
}
}
If T::f is never called, or is never called with a positive value,
its local static variable u is never created (and of course never
destroyed).
The first time T::f is called (on behalf of any T object) with
a positive value, its local static variable u is created. It will
be destroyed automatically at program end, as part of exit
processing.
Details of the order of destruction of static objects at program end
are complicated. It's a good idea to write your program so that the
exact point of destruction during exit processing doesn't matter.
--
Steve Clamage, stephen.clamage@sun.com
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: daniel@reichardt.ch (Daniel Hartmeier)
Date: 1999/10/14 Raw View
On 14 Oct 1999 16:35:39 GMT, "Peter de Leeuw van Weenen"
<pleforum@orion.nl> wrote:
> What is the lifetime of a static variable defined in a member function of a
> class? Is it equal to the lifetime of the class, or is it equal to the
> lifetime of the program?
> I can test it with my C++ compiler, but I want to know if this is formalized
> in the lanquage?
9.4.2 Static data members [class.static.data]
3 [Note: once the static data member has been defined, it exists even
if no objects of its class have been created. [Example: in the
example above, run_chain and running exist even if no objects of
class process are created by the program. ]]
class process {
static process* run_chain;
static process* running;
};
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: daniel@reichardt.ch (Daniel Hartmeier)
Date: 1999/10/14 Raw View
On 14 Oct 1999 16:35:39 GMT, "Peter de Leeuw van Weenen"
<pleforum@orion.nl> wrote:
> What is the lifetime of a static variable defined in a member function of a
> class? Is it equal to the lifetime of the class, or is it equal to the
> lifetime of the program?
> I can test it with my C++ compiler, but I want to know if this is formalized
> in the lanquage?
Ah, you mean a static local variable in a member function of a class
(and not a static data member of the, as I mistakenly assumed in my
previous post)
I think the following general paragraphs apply in this case, too:
3.7.1 Static storage duration [basic.stc.static]
4 The keyword static can be used to declare a local variable with
static storage duration. [Note: {...} 3.6.3 describes the
destruction of local static variables. ]
3.6.3 Termination [basic.start.term]
1 Destructors for initialized objects of static storage duration
(declared at block scope or at namespace scope) are called as a
result of returning from main and as a result of calling exit.
And specifically regarding member functions:
9.3 Member functions [class.mfct]
6 A static local variable in a member function always refers to the
same object, whether or not the member function is inline.
Would it be correct to say that the only difference between a static
data member of a class and a local static variable defined in one of the
class' member functions is that latter is only visible inside its
defining block (regarding storage duration, or in every aspect)?
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Matt Frantz <mhf@micron.com>
Date: 1999/10/14 Raw View
Peter de Leeuw van Weenen wrote:
> Hello ,
>
> Please, can someone answer the question:
>
> What is the lifetime of a static variable defined in a member function of a
> class? Is it equal to the lifetime of the class, or is it equal to the
> lifetime of the program?
>
> I can test it with my C++ compiler, but I want to know if this is formalized
> in the lanquage?
>
"static" variables persist for the duration of the program.
Regards,
Matt Frantz
Micron Tech. TX
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Jim Hyslop <jim.hyslop@leitch.com>
Date: 1999/10/14 Raw View
In article <7u44td$1lk5$1@buty.wanadoo.nl>,
"Peter de Leeuw van Weenen" <pleforum@orion.nl> wrote:
>
> Hello ,
>
> Please, can someone answer the question:
>
> What is the lifetime of a static variable defined in a member function
> of a
> class? Is it equal to the lifetime of the class, or is it equal to the
> lifetime of the program?
The lifetime of the program. The variable has "static storage
duration", which is independent of whether or not the variable is a
member function.
--
Jim
I ignore all email from recruitment agencies.
Please do not send me email with questions - post
here.
Sent via Deja.com http://www.deja.com/
Before you buy.
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Matt Frantz <mhf@micron.com>
Date: 1999/10/15 Raw View
Daniel Hartmeier wrote:
> On 14 Oct 1999 16:35:39 GMT, "Peter de Leeuw van Weenen"
> <pleforum@orion.nl> wrote:
>
> > What is the lifetime of a static variable defined in a member function of a
> > class? Is it equal to the lifetime of the class, or is it equal to the
> > lifetime of the program?
>
> [snip]
>
> Would it be correct to say that the only difference between a static
> data member of a class and a local static variable defined in one of the
> class' member functions is that latter is only visible inside its
> defining block (regarding storage duration, or in every aspect)?
The static variables defined in a member function are initialized the first time
they go into scope. Static data members are initialized at startup.
Regards,
Matt Frantz
Micron Tech. TX
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Peter de Leeuw van Weenen" <pleforum@orion.nl>
Date: 1999/10/14 Raw View
Hello ,
Please, can someone answer the question:
What is the lifetime of a static variable defined in a member function of a
class? Is it equal to the lifetime of the class, or is it equal to the
lifetime of the program?
I can test it with my C++ compiler, but I want to know if this is formalized
in the lanquage?
Peter de Leeuw
Software Engineer
pleforum@orion.nl
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]