Re: Is There Any Reason to Even Use VC++ Anymore?
- From: "Josh McFarlane" <darsant@xxxxxxxxx>
- Date: 7 Dec 2005 13:08:06 -0800
Peter Oliphant wrote:
> > Honestly why use gc at all? Never got why people felt the need for a
> > garbage collector.
> Now, how can the class know whether the instance has further use externally?
> Answer: in general, it can't! A class, is typically unaware of the
> environment it is in, since many classes are designed for re-usability in
> DIFFERENT environments.
Contracts between the calling function and the destination functions
work quite nicely for me.
If I require an object in a function, I use a reference.
If, for some reason, the object needs to be worried about the life of
the object (Such as a "message" being passed), I make that a condition
of the function if it is the end-point and destroy the object.
If, for another reason, the calling function needs to allocate memory,
call a 2nd function, and then is done with the memory, it is up to the
calling function to remove the memory from play.
> An example. Say a class is responsible for displaying the color of an object
> representing a toy ball. It does this by taking a pointer to a ball object
> and displaying its color. Now, here's the problem. The caller could have
> passed a pointer to the instance of the ball itself, or a COPY of this
> instance. In the first case, the instance should not be destroyed. In the
> second case, the instance passed MUST be destroyed. But the class has no
> idea which case is true for any particular call!
If the function is responsible for drawing the ball, it should be up to
the caller function to determine whether or not the allocated object is
through. I mean granted it's around for another cycle or two until you
hit the delete function, but the cost is negligible.
I tend to shun away from using lists of pointers, and generally don't
have that big of a problem with it. I just go with the philosophy of
the allocator owns the pointer, unless it calls another function which
always assumes ownership of the allocated pointer, and that the owner
is responsible for freeing the memory.
Can you give an example where memory would have to be allocated prior
to the function, but the function would require both pass-through and
delete functionality that the calling function could not provide?
.
- Follow-Ups:
- Re: Is There Any Reason to Even Use VC++ Anymore?
- From: Andre Kaufmann
- Re: Is There Any Reason to Even Use VC++ Anymore?
- From: Peter Oliphant
- Re: Is There Any Reason to Even Use VC++ Anymore?
- References:
- Re: Is There Any Reason to Even Use VC++ Anymore?
- From: Herby
- Re: Is There Any Reason to Even Use VC++ Anymore?
- From: Peter Oliphant
- Re: Is There Any Reason to Even Use VC++ Anymore?
- From: Willy Denoyette [MVP]
- Re: Is There Any Reason to Even Use VC++ Anymore?
- From: Peter Oliphant
- Re: Is There Any Reason to Even Use VC++ Anymore?
- From: Willy Denoyette [MVP]
- Re: Is There Any Reason to Even Use VC++ Anymore?
- From: Peter Oliphant
- Re: Is There Any Reason to Even Use VC++ Anymore?
- From: Brian Muth
- Re: Is There Any Reason to Even Use VC++ Anymore?
- From: Peter Oliphant
- Re: Is There Any Reason to Even Use VC++ Anymore?
- From: Josh McFarlane
- Re: Is There Any Reason to Even Use VC++ Anymore?
- From: Peter Oliphant
- Re: Is There Any Reason to Even Use VC++ Anymore?
- Prev by Date: Re: \CLR Mixed mode compilation woes!
- Next by Date: CComPtr pass by reference issue with VC 2005 compiler
- Previous by thread: Re: Is There Any Reason to Even Use VC++ Anymore?
- Next by thread: Re: Is There Any Reason to Even Use VC++ Anymore?
- Index(es):
Relevant Pages
|