static object/variable

J

junw2000

Is it possible to destruct a static object or variable before the
program terminate?

Thanks
 
S

Sebastian Redl

Is it possible to destruct a static object or variable before the
program terminate?

You can call the destructor explicitly, but it will be called again when the
program terminates, so no, I wouldn't advise you to do that.

If you have to, use something pointer-like and use new and delete for
explicit allocation/de-allocation.

E.g. a boost::scoped_ptr or a std::auto_ptr or even a boost::eek:ptional.
(Doesn't use the heap.)
 
I

Ian Collins

Is it possible to destruct a static object or variable before the
program terminate?
Possible, yes. Desirable, no. The object will be destroyed twice.

Maybe you want a singleton rather than a static object?
 
J

junw2000

Possible, yes. Desirable, no. The object will be destroyed twice.

Maybe you want a singleton rather than a static object?

Singleton is still implemented using static variable, right? What is
the advantage of singleton than static variable?

Thanks.
 
I

Ian Collins

*Please* don't quote signatures.
Singleton is still implemented using static variable, right? What is
the advantage of singleton than static variable?
Not necessarily.

There are other ways of implementing a singleton, such as using an
object with a static pointer member.
 

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

No members online now.

Forum statistics

Threads
473,801
Messages
2,569,658
Members
45,427
Latest member
Fredrick95

Latest Threads

Top