[openbeos] Re: Pointer or reference?

  • From: "Scott MacMaster" <scott@xxxxxxxxxxxxxxxxxx>
  • To: <openbeos@xxxxxxxxxxxxx>
  • Date: Wed, 31 Mar 2004 16:12:36 -0500

> >Unfortunately, the BeAPI adds more places where pointers must be used
mainly
> >because some BeAPI objects delete themselves (why anyone would make
objects that
> >behave like that is beyond me).
>
> Actually, this is a good thing. Other environments work too like this. In
delphi, for example, objects delete themselves if they are part of a visual
hierarchy. Otherwise you would have to keep track of all the objects (mostly
views) you add (or keep a pointer to them in the parent class, which isn't
needed, since you can use FindView() instead).

First, I would not want my objects to automatically be destroyed when they
are removed from the hierarchy, because I may want to use the object
somewhere else.

I'm not familiar with these other API's, however I still say it's a bad
thing.  For one thing it forces you C++ differently then it was intended.
Objects you explicitly allocate memory for should be explicitly deallocated.
The biggest issue this introduces, is that you can't trust any pointer to
the object.

You would have to manually destroy objects you created dynamically.  Which,
of course could be done by using FindView() or a stored pointer.  For, the
few cases where it would be more convenient to create a object dynamically
it would also be more convenient to store the pointer rather then repeated
use if FindView().  For the majority of the time your could just create
objects on the stack and they will be implicitly destroyed when the parent
is destroyed.

The only time where automatic deletion of any kind is ok is in garbage
collection.


Later,
Scott MacMaster


Other related posts: