Answer the question
In order to leave comments, you need to log in
What objects to delete in the destructor?
Hi all.
I wondered what objects should be removed.
For example, I remove widgets and fields.
And whether it is necessary to delete int or double. And why ?
I would like to read on this topic, maybe someone = what will advise
Thanks
Answer the question
In order to leave comments, you need to log in
After the body of the destructor has completed, destructors are automatically called for all fields of the object, in reverse order.
Int and double have zero destructors - but others are not needed.
In what cases it is necessary to write the destructor ourselves...
1. We own some resource, but the regular destructor does not destroy it.
• Simple (non-smart) pointer and allocated memory - this is well described by sitev_ru .
• An object-blocker, for example, a mutex (a mutex is an inter-thread synchronization primitive that prevents two threads from entering certain sections of the code at the same time).
class Mutex {
public:
void enter();
void leave();
}
class Lock {
public:
Lock(Mutex& aMutex) : mutex(aMutex) { mutex.enter(); }
~Lock() { mutex.leave(); }
private:
Mutex& mutex;
}
…
Mutex mutex;
{ Lock lock(mutex);
// всё, что здесь, выполняется внутри мьютекса.
// Даже если выпадет авария, из мьютекса корректно выйдем.
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question