これは、標準 C++ で Final のような動作を実装する良い方法ですか? (特別なポインタなし)
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;
}
- オブジェクトによって例外がスローされない -> NoException -> オブジェクトがクリーンアップされる
- オブジェクトによってスローされた例外 -> 処理済み -> NoException -> オブジェクトのクリーンアップ
- オブジェクトによってスローされた例外 -> スローされた -> 例外 -> オブジェクトがクリーンアップされた -> スローされた