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

问题描述

C ++支持块?



什么是?

解决方案

C ++不支持'finally'块。原因是C ++反而支持RAII:资源获取是初始化 - 一个糟糕的名称 一个非常有用的概念。



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



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

  //实现类RAII 
class lock
{
mutex& m_;

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

//使用'mutex'和'lock'对象的类
class foo
{
mutex mutex_; //用于锁定'foo'对象的互斥锁
public:
void bar()
{
lock scopeLock(mutex_); //锁定对象。

foobar(); //可能抛出异常的操作

//即使发生异常
//,也会析构scopeLock,这将释放互斥体并允许
//其他函数锁定对象并运行。
}
};

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



对于那些拥有C#或VB.NET的家族,您可能会认识到RAII是类似的。事实上,这两种方法非常相似。主要区别是RAII将确定性地释放任何类型的资源 - 包括内存。当在.NET(甚至.NET语言C ++ / CLI)中实现IDisposable时,资源将被确定性地释放,除了内存。在.NET中,内存不是确定性释放;



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


Does C++ support 'finally' blocks?

What is the RAII idiom?

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

解决方案

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.

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.

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 also simplifies using objects in 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 a 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.)

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 be deterministically released; memory is only released during garbage collection cycles.

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

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

10-27 20:00