Re: Delphi interfaces vs. COM interfaces

From: Harley Pebley (harley_pebley_at_idahotech.com)
Date: 12/11/03


Date: Wed, 10 Dec 2003 16:58:52 -0700


> Or change your function signature so that it takes a const interface
> variable.
>
> Instead of
> function TMyObject.Myfunc(Value: ILockable): Boolean;
> use
> function TMyObject.Myfunc(const Value: ILockable): Boolean;
>
> Delphi passes it a copy so it can't be changed, when the
> function returns, it decrements the reference count of the COPY.
> The original goes on it's merry way.
> The advantage to this method, is that only the function that uses
> the interface has to change. The clients don't even need to be aware
> that they are passing an interface.

In the context of mixing object references and interface references,
counting on the const to not cause an object reference to go out of scope
is pretty dangerous. I wouldn't recommend using it. Reason is, the const
only keeps the reference count from being incremented on entry to the
function and decremented on exit. If the function being called does
anything which would cause the count to change (such as assigning the
parameter to an internal variable, using a with statement, calling another
function with it as a parameter that is not const, etc.), then there will
be problems.

Regards,
Harley Pebley



Relevant Pages

  • Re: Anders Hejlsberg comment on immutable objects
    ... >explicit interface implementation exists is so an interface name can class ... Improper implicit conversions constitute a nasty ... >> With value types, the variable's value is the object, not a reference ... Consider the affects of our different views on a const ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Anders Hejlsberg comment on immutable objects
    ... >>explicit interface implementation exists is so an interface name can ... I wouldn't cast to an ... > variable as a sort of implicit reference, ... readonly const MyStruct myInstance. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Anders Hejlsberg comment on immutable objects
    ... because just passing in a reference by value doesn't ... >example of a situation where a read interface does help out. ... >and trust the server method, you simply pass the full blown object. ... with a short comparison of const and read only ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Interfaces and Classes, where do I ask about the differences?
    ... how am I mixing interface with object references? ... What interface reference would I have ... that the compiler generates code to call _Release on FDocumentInterface ...
    (borland.public.delphi.non-technical)
  • Re: 7.0 wishlist?
    ... The "auto-implement" is intended mainly for the odd situation where an existing class you can't edit fits some interface and you're willing to take responsibility for it if it turns out not to actually adhere to the contract, and try using it where that interface type is expected. ... If reference declarations started showing up with the odd asterisk, bang, or other punctuation mark on it, but never primitive declarations, people would probably be able to guess what was going on, on the basis of "what other binary flag might be set on references but not primitives and would be really useful besides can be/cannot be null?" ... the compiler cannot prove by static analysis that the RHS isn't null might be a good idea. ... Object foo, bar; ...
    (comp.lang.java.programmer)