No, it will not. Reference counting with interfaces only works if you use interfaces as referencing types (so it has to be 'var MyObject: IUnknown'), all class-typed variables will not counted. I think this is because all class-typed variables are simply pointers and an interface variable is handled special (like a smart-pointer-structure in C++, which capsulate a pointer and does the reference counting automatically for you; this works, because a smart-pointer is constructed on the heap and so, it will automatically call the destructor if it goes out of scope).

Is there a way to make a smart-pointer in pascal? Is it possible to create objects on the heap so that they will get destroyed and freed automatically when they get out of scope? Or is there a way to get informed if a class-typed variable get out of scope?
I think it doesn't look so clean, if I use interfaces to manage reference counting. In my case it is not possible to do without this, because there is no efficient way to decide if there are other references to an object.

If I would rewrite my program to use interfaces everywhere, is there a way to mix predefined functions (which are often uses other abstract functions in the background, but give an easier way to the user to do a few things and if there is no real reason, they would be implemented in all classes the same) and abstract functions (the default in interfaces). If this is possible (how? all my functions are abstract by default), is it possible to make some helper-functions private or protected?
You see, I thought, this is not possible, that is the reason, why I uses (partly) abstract classes at the moment.

Albert


Am Donnerstag, den 17.08.2006, 08:41 +0200 schrieb Vincent Snijders:
Cesar Romero schreef:
> 
>>> Is there a way to use the reference counting (used with interfaces) with
>>> normal objects?
>>>     
>> Currently not, unless you use TInterfacedObject.
>>
>>   
> But you have to declare as Interface
> 
> 

I guess TInterfacedObject implemets IUnknown, so you don't need to declare a interface.

type
   TMyClass = class(TInterfaceObject)
   // all your things.
   end;

var
   MyObject : TMyClass;

MyObject will be reference counted.

Vincent

_________________________________________________________________
     To unsubscribe: mail [EMAIL PROTECTED] with
                "unsubscribe" as the Subject
   archives at http://www.lazarus.freepascal.org/mailarchives

Reply via email to