I have code that controls a mutex lock/unlock based on scope:
void PerformLogin() { ScopeLock < Lock > LoginLock( &m_LoginLock ); doLoginCommand(); ScopeLock < SharedMemoryBase > MemoryLock( &m_SharedMemory ); doStoreLogin(); ... }
Can I guarantee that MemoryLock
will be destructed before LoginLock
?
The body of an object's destructor is executed, followed by the destructors of the object's data members (in reverse order of their appearance in the class definition), followed by the destructors of the object's base classes (in reverse order of their appearance in the class definition).
Objects are always destroyed in reverse order of their creation. The reason for reverse order is, an object created later may use the previously created object.
A destructor is a member function that is invoked automatically when the object goes out of scope or is explicitly destroyed by a call to delete . A destructor has the same name as the class, preceded by a tilde ( ~ ).
Destructors are usually used to deallocate memory and do other cleanup for a class object and its class members when the object is destroyed. A destructor is called for a class object when that object passes out of scope or is explicitly deleted.
Yes, it is. In any particular scope local objects are destroyed in the reverse order that they were constructed.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With