John Levon <[EMAIL PROTECTED]> writes:

| On Mon, Mar 11, 2002 at 11:24:49PM +0100, Lars Gullik Bjønnes wrote:
>
>> Always perform unmanaged resource acquisition in the constructor body,
>> never in initializer lists.
>
| Can you please give an example for the brain dead amongst us ?

class Foo {
      // unsafe variant
      Foo()
      : bar1(new Bar), bar2(new Bar)
      {}
      // safer variant
      Foo() {
            bar1 = new Bar;
            bar2 = new Bar;
      }
private:
        Bar * bar1;
        Bar * bar2;
};


For the unsafe constructor try to think how you can avoid a memory
leak if the second new throws.

-- 
        Lgb

Reply via email to