When does the destructor of a static object gets called?

A

Allerdyce.John

Hi,

Can you please tell me when does the destructor of a static object gets
called?

Thank you.
 
B

Bob Hairgrove

Hi,

Can you please tell me when does the destructor of a static object gets
called?

Thank you.

Why do you ask here? It's in your textbook, isn't it?
 
M

Matthias Kluwe

Hi!

(e-mail address removed):
Can you please tell me when does the destructor of a static object gets
called?

You mean objects in unnamed namespaces like

#include <iostream>
#include <ostream>

class A {
public:
~A() { std::cout <<"~A"; }
};

namespace {
A a;
}

int main() {
return 0;
}

What do you guess? Did you try out?

Regards,
Matthias
 
V

Victor Bazarov

Matthias said:
(e-mail address removed):
Can you please tell me when does the destructor of a static object gets
called?


You mean objects in unnamed namespaces [...]

I would guess 'no'. Usually "a static object" means an object with static
_storage_duration_.

V
 
R

rino100

A static object is created as the programs starts to run upon
allocation of all global variables... therefore it is destroyed when
all global variables are deallocated (ie. at program exit)
 
V

Victor Bazarov

A static object is created as the programs starts to run upon
allocation of all global variables... therefore it is destroyed when
all global variables are deallocated (ie. at program exit)

A static object can be declared inside a function, and be never created
if that function is never called. And as a result, such object is never
destroyed. How 'bout that?

V
 
B

Ben Pope

Diego said:
are you suggesting the destructor is never called?

Please quote what you are referring to.

He is saying that the object may never be constructed. In that case,
the destructor will not be called, of course.

Ben Pope
 

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
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top