resource acquisition is initialization "RAII"

Posted by hitech on Stack Overflow See other posts from Stack Overflow or by hitech
Published on 2010-03-20T17:28:26Z Indexed on 2010/03/20 17:31 UTC
Read the original article Hit count: 366

Filed under:
|

in the example below

class X{
 int *r;
 public: 
X(){cout<< X is created ; r new int[10]; }
 ~X(){cout<< X is destroyed ; delete [] r; }
};
class Y
{
 public: 
Y(){ X x; throw 44; } 
~Y(){cout<< Y is destroyed ;}
};

I got this example of RAII from one site and ave some doubts. please help.

  1. in the contructor of x we are not considering the scenation "if the memory allocation fails" .
  2. Here for the destructor of Y is safe as in y construcotr is not allocating any memory. what if we need to do some memory allocation also in y constructor?

© Stack Overflow or respective owner

Related posts about c++

Related posts about raii