[fpc-devel] Suggestion: reference counted objects

Sven Barth pascaldragon at googlemail.com
Sun Sep 21 14:38:50 CEST 2014


On 21.09.2014 14:15, Chriss Kalogeropoulos wrote:
> Hi Sven,
>
> This design means that old classes need to be subclassed in order to
> enable this feature,  correct?
> Would it be possible instead of declaring this on the class definition,
> to declare it on the actual variable? Something like
>
> var aArcObj: TFooObj; refcounted;
>
> Or similar.
>
> This would allow the same classes to be used both ways.

No. You would need to have a place where to store the reference count 
and one of the objections some people have is the size increase of 
TObject if it would get a RefCount field (which would be the only 
solution with your approach). Also you'd need to take care that all 
variables where you pass along your object instance are marked as 
refcounted. This just complicates things too much (both for programmers 
using this as well as for the compiler).

> Also how about a smart pointer record  implementation with generics? Is
> it possible ?

What's missing is the possiblity to hook into the initialization (unlike 
interfaces, strings and arrays class variables fields are not 
initialized with Nil) and finalization (you need to adjust the reference 
count) of records. And also it's currently not possible to overload the 
assignment operator if both types have the same record type.

Regards,
Sven



More information about the fpc-devel mailing list