Re: Good style for memory management?



Is there any particular reason you're not passing the args to the ctor? As we all know C++ rather likes init-with-ctor-rather-than-init()-method
so wouldn't it be more sensible/prudent to just pass it to the ctor like

    MyFancyWidget* w = 0;

    try {
        w = manage (new MyFancyWidget(some, args, it, might, need));
    }
    catch (....

(Assuming that after something has been caught you'd interrupt whatever is going on there anyway, otherwise you'd still have to check for w being 0/NULL)


On 3/18/07, Paul Davis <paul linuxaudiosystems com> wrote:
On Sun, 2007-03-18 at 17:50 +0100, Roland Schwarz wrote:

> that if finding an error in the file throws out. Since you are not the
> author of the parser you either have to
> *) wrap everything in try {} catch() {} blocks or use
> *) resource acquisition is initialization idiom.

most good books on C++ that i have read which tackle this issue
recommend avoiding throwing out of constructors anyway. i think meyers
and others recommend constructing an object as minimally as possible and
then explicitly "initializing" it. this is in part precisely to deal
with the kind of ambiguity that you are encountering. thus, if you have

  class MyFancyWidget : public class Gtk::SomeWidget {.... }

you construct such a thing like this:

     MyFancyWidget* w = manage (new MyFancyWidget()); // does almost
nothing

     try {
         w->init (some, args, it, might, need);
     }

     catch (....

since this allows you to handle exceptions cleanly and clearly.


_______________________________________________
gtkmm-list mailing list
gtkmm-list gnome org
http://mail.gnome.org/mailman/listinfo/gtkmm-list



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]