最后在C ++中 [英] Finally in C++

查看:114
本文介绍了最后在C ++中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是在标准C ++中实现类似终极的行为的好方法吗?
(没有特殊指针)

  class异常:​​public Exception 
{public:virtual bool isException {return true; }};

class NoException:public Exception
{public:bool isException(){return false; }};


对象* myObject = 0;

try
{
// OBJECT CREATION AND PROCESSING
try
{
myObject = new Object();

//用myObject做一些事情。
}

// EXCEPTION HANDLING
catch(异常& e)
{
//当有异常,处理或抛出
// else将抛出NoException。
}

throw NoException();
}

// CLEAN UP
catch(异常& e)
{
删除myObject;

if(e.isException())throw e;
}




  1. 对象抛出的异常 - > NoException - >对象清理

  2. 对象抛出的异常 - >处理 - > NoException - >清除对象

  3. 对象抛出的异常 - >抛出 - >例外 - >对象清理 - >抛出


解决方案

标准答案是使用一些变体资源分配初始化缩写为RAII。基本上,你构造一个变量,它具有与finally之前的块内的块相同的范围,然后在对象析构函数的finally块中进行工作。

  try {
//有些工作
}
finally {
//清理代码
}

成为

  class Cleanup 
{
public:
〜清理()
{
//清理代码
}
}

清理清理

//有些工作。

这看起来非常不方便,但通常有一个预先存在的对象会为您清理。在你的情况下,看起来你想要破坏finally块中的对象,这意味着一个智能或自动指针会做你想要的:

 code>的std :: auto_ptr的<对象> obj(new Object()); 

无论抛出什么异常,对象都将被破坏。回到RAII,在这种情况下,资源分配是为Object分配内存并构造它,初始化是auto_ptr的初始化。


Is this a good way to implement a Finally-like behavior in standard C++? (Without special pointers)

class Exception : public Exception
    { public: virtual bool isException() { return true; } };

class NoException : public Exception
    { public: bool isException() { return false; } };


Object *myObject = 0;

try
{
  // OBJECT CREATION AND PROCESSING
  try
  {
    myObject = new Object();

    // Do something with myObject.
  }

  // EXCEPTION HANDLING
  catch (Exception &e)
  {
    // When there is an excepion, handle or throw,
    // else NoException will be thrown.
  }

  throw NoException();
}

// CLEAN UP
catch (Exception &e)
{
  delete myObject;

  if (e.isException()) throw e;
}

  1. No exception thrown by object -> NoException -> Object cleaned up
  2. Exception thrown by object -> Handled -> NoException -> Object cleaned up
  3. Exception thrown by object -> Thrown -> Exception -> Object cleaned up -> Thrown

解决方案

The standard answer is to use some variant of resource-allocation-is-initialization abbreviated RAII. Basically you construct a variable that has the same scope as the block that would be inside the block before the finally, then do the work in the finally block inside the objects destructor.

try {
   // Some work
}
finally {
   // Cleanup code
}

becomes

class Cleanup
{
public:
    ~Cleanup()
    {
        // Cleanup code
    }
}

Cleanup cleanupObj;

// Some work.

This looks terribly inconvenient, but usually there's a pre-existing object that will do the clean up for you. In your case, it looks like you want to destruct the object in the finally block, which means a smart or auto pointer will do what you want:

std::auto_ptr<Object> obj(new Object());

No matter which exceptions are thrown, the object will be destructed. Getting back to RAII, in this case the resource allocation is allocating the memory for the Object and constructing it and the initialization is the initialization of the auto_ptr.

这篇关于最后在C ++中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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