what is a destructor used for?

what is a destructor used for?

when writing a .cpp & .h

if I use the constructor to map my pins etc

when do we use destructors and how would they be used

is it for implementing another library to combine it with the one we are using? ? ?

what is a destructor used for?

It gets called when an object is explicitly deleted, or implicitly (when going out of scope) deleted.

when do we use destructors

Always, when the object is (to be) deleted. If you don’t define on, the compiler will.

and how would they be used

The destructor gets called when the instance is explicitly deleted (delete pObject;) or when the object goes out of scope.

is it for implementing another library to combine it with the one we are using?

Neither.

?

The question that you didn’t ask is why does one need to explicitly define a destructor, if the compiler will create one for you.

Suppose that in your instance, you dynamically allocate some memory, to store a list of names of files, for instance.

Your object (or instance) goes out of scope (or is deleted). The memory you allocated is NOT freed just because the object (or instance) is deleted. The destructor is supposed to do that.

so say I have a light to flash in my constructor to get someone's attention

but they are blind

so I need to use a speaker/buzzer instead

I might as well turn my light off (disable it)

and just use my speaker/buzzer

is that what your getting at?