当对象在堆栈上声明时

当对象在堆栈上声明时

本文介绍了当对象在堆栈上声明时,你能保证析构顺序吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有基于范围控制互斥锁/解锁的代码:

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();

    ...
}

code> MemoryLock 将在 LoginLock ?之前被销毁

Can I guarantee that MemoryLock will be destructed before LoginLock?

推荐答案

是的,是的。在任何特定范围中,本地对象以与构造它们相反的顺序被销毁。

Yes, it is. In any particular scope local objects are destroyed in the reverse order that they were constructed.

这篇关于当对象在堆栈上声明时,你能保证析构顺序吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 19:31