Angus Leeming <[EMAIL PROTECTED]> writes: | On Monday 12 August 2002 4:36 pm, Lars Gullik Bjønnes wrote: >> Angus Leeming <[EMAIL PROTECTED]> writes: >> | ====== >> | +#include "counters.h" >> | >> | Buffer::Buffer(string const & file, bool ronly) >> | >> | : niceFile(true), lyx_clean(true), bak_clean(true), >> | >> | unnamed(false), dep_clean(0), read_only(ronly), >> | - filename_(file), users(0) >> | + filename_(file), users(0), ctrs(new Counters) >> >> Never allocate memory in initializtors... you are unagle to handle >> exceptions properly. >> >> Always allocate in body of constructor. > | Sorry, Lars, I don't believe you: > | Herb Sutter, "Exceptional C++", pag 28:
Now go read "More Exceptional C++" :-) (or perhaps I got it exaclty opposite...) I'll dig out some references for you. | All right, I'll admit it: ... What I'd actually prefer to write is: > | template<class T> | Stack<T>::Stack() | : v_(new T[10]), // default allocation | vsize)(10), | vused_(0) // nothing used yet | { | } The real problem comes when you allocate _two_ pointers. class Foo { public: Foo() : a(new A), b(new B) {} private: A * a; B * b; }; now let "new B" trow... How to free "a"? (But I belive the discussion will show that there are ways to make it work.) -- Lgb