Re: [c++] passing parameters and safety in deletion.
From: Anthony Borla (ajborla_at_bigpond.com)
Date: 02/06/05
- Next message: Alwyn: "Re: friend ostream& operator<< (ostream&, Array<T>&);"
- Previous message: George Huber: "Re: Can't understand this! (Help required please)"
- In reply to: Val: "Re: [c++] passing parameters and safety in deletion."
- Next in thread: Chris \( Val \): "Re: [c++] passing parameters and safety in deletion."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sun, 06 Feb 2005 06:58:15 GMT
"Val" <valmont_programming@hotmail.com> wrote in message
news:42059a3b$0$44104$5fc3050@dreader2.news.tiscali.nl...
>
> | You're asking about whether a 'OneClass' object has acquired
> | ownership of an 'OtherClass' object via a pointer to that object,
> | hence is responsible for 'deleting' that object when it reaches the
> | end of its own lifetime [i.e. does the 'deleting' when its destructor
> | is triggered] ?
>
> No I don't ask that. We covered this recently. I took the point
> then.
>
> The requirements are explicitly:
>
> 1) obj.AcceptClass(new OtherClas);
> 2) obj.AcceptClass(&otherclassObj); //not allocated dynamically.
> 3) obj->AcceptClass(new OtherClass); //obj is created dynamically.
>
Only 1) and 2), [free store-based object pointer, stack-based object
pointer, respectively] are significant here. 3) is superfluous in that the
caller's location [stack or free store] has no impact on 'AcceptClass'
behaviour.
>
> No "bool" value. That is so ugly.
>
Fair enough. However, in all cases you are passing a raw pointer argument to
'AcceptClass'. This means:
* Both function overloading and template-based 'tricks'
cannot be applied [because there is no type-based
difference: a pointer is a pointer !]
* Without such explicit information the compiler has no way
of determining whether it is a free store address, or that of
a stack-based object, nor whether it 'owns' the referenced
object or not, hence should, or should not, perform a 'delete'.
I, personally, see no way other than supplying explicit,
behaviour-modifying, information. Other posters may have alternative views.
>
> - Perhaps is one of the famous cases to where to overload
> the "new" operator?
>
I don't see how this could help. In particular, what can you do in an
overloaded 'new' [whether global, or class-specific] that will affect
whether a 'delete' will, or will not, be performed ?
>
> - Or perhaps there is way to do tricks with static members or
> something. I heard a bell ringing once but I have no clue
> about the technique. Something with reference counting
> perhaps?
>
Building a class or co-operating classes to manage memory allocations /
deletions, which is what you seem to be alluding, is another ballgame. Also,
even if this were done, I don't see how you could make 'AcceptClass' calls
as you outlined earlier.
Personally, I would revert to a smart pointer-based design, whether this is
custom-written [not at all difficult to do], or from a third-party library
like 'boost'. I would rather get on with the job of programming rather than
get too involved in creating or using esoteric tricks which is why, these
days, I tend to prefer using [where I can] higher level languages [like
Scheme] or scripting languages [like REXX].
I hope this helps.
Anthony Borla
- Next message: Alwyn: "Re: friend ostream& operator<< (ostream&, Array<T>&);"
- Previous message: George Huber: "Re: Can't understand this! (Help required please)"
- In reply to: Val: "Re: [c++] passing parameters and safety in deletion."
- Next in thread: Chris \( Val \): "Re: [c++] passing parameters and safety in deletion."
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|