C ++是否支持'finally'块? (这个我听到的“RAII”是什么?) [英] Does C++ support 'finally' blocks? (And what's this 'RAII' I keep hearing about?)

查看:190
本文介绍了C ++是否支持'finally'块? (这个我听到的“RAII”是什么?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

C ++是否支持 终于 '阻止?

Does C++ support 'finally' blocks?

什么是 RAII成语

C ++的RAII成语与 C#的'使用'语句

What is the difference between C++'s RAII idiom and C#'s 'using' statement?

推荐答案

不,C ++不支持'finally'块。原因是C ++反而支持RAII:资源获取正在初始化 - 一个恶劣的名字 是一个非常有用的概念。

No, C++ does not support 'finally' blocks. The reason is that C++ instead supports RAII: "Resource Acquisition Is Initialization" -- a poor name for a really useful concept.

想法是,对象的析构函数负责释放资源。当对象具有自动存储持续时间时,当创建对象的析构函数被创建时,它将被调用 - 即使该块在异常存在的情况下退出。这是主题的 Bjarne Stroustrup的说明

The idea is that an object's destructor is responsible for freeing resources. When the object has automatic storage duration, the object's destructor will be called when the block in which it was created exits -- even when that block is exited in the presence of an exception. Here is Bjarne Stroustrup's explanation of the topic.

RAII的常见用法是锁定互斥体:

A common use for RAII is locking a mutex:

// A class with implements RAII
class lock
{
    mutex &m_;

public:
    lock(mutex &m)
      : m_(m)
    {
        m.acquire();
    }
    ~lock()
    {
        m_.release();
    }
};

// A class which uses 'mutex' and 'lock' objects
class foo
{
    mutex mutex_; // mutex for locking 'foo' object
public:
    void bar()
    {
        lock scopeLock(mutex_); // lock object.

        foobar(); // an operation which may throw an exception

        // scopeLock will be destructed even if an exception
        // occurs, which will release the mutex and allow
        // other functions to lock the object and run.
    }
};

RAII还简化了将对象用作其他类的成员。当拥有类被破坏时,由RAII类管理的资源被释放,因为RAII管理类的析构函数被调用。这意味着当您为管理资源的类中的所有成员使用RAII时,您可以使用非常简单,甚至是所有者类的默认析构函数,因为它不需要手动管理其成员资源生命周期。 (感谢Mike B 指出这一点。)

RAII also simplifies using objects as members of other classes. When the owning class' is destructed, the resource managed by the RAII class gets released because the destructor for the RAII-managed class gets called as a result. This means that when you use RAII for all members in a class that manage resources, you can get away with using a very simple, maybe even the default, destructor for the owner class since it doesn't need to manually manage its member resource lifetimes. (Thanks to Mike B for pointing this out.)

对于那些具有C#或VB.NET的家族,您可能会认识到RAII类似使用IDisposable和 .NET确定性破坏'using'statement 。的确,这两种方法非常相似。主要区别在于RAII将确定性地释放任何类型的资源 - 包括内存。当在.NET中实现IDisposable(即使是.NET语言C ++ / CLI)时,除了内存之外,资源将被确定性地释放。在.NET中,内存不是确定性地释放的;记忆只能在垃圾收集循环中释放。

For those familliar with C# or VB.NET, you may recognize that RAII is similar to .NET deterministic destruction using IDisposable and 'using' statements. Indeed, the two methods are very similar. The main difference is that RAII will deterministically release any type of resource -- including memory. When implementing IDisposable in .NET (even the .NET language C++/CLI), resources will be deterministically released except for memory. In .NET, memory is not deterministically released; memory is only released during garbage collection cycles.

†有些人认为破坏是资源放弃是RAII成语的更准确的名称。

† Some people believe that "Destruction is Resource Relinquishment" is a more accurate name for the RAII idiom.

这篇关于C ++是否支持'finally'块? (这个我听到的“RAII”是什么?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆