George Birbilis wrote:
If so, what side-effects would this cause when applied across the
complete class-hierarchy?
Blow up the size of you executable.
And a virtual method call is slower than a non-virtual call.
(as it needs to do a few lookups: class, VMT)
To ensure self is not nil, you can insert a check in a method
itself. However, you cannot defend yourself against an
invalid (but non-nil) object pointer. (I.e. a pointer to an
object that is freed already.)
That's why you use a class method. In Delphi you can call a class method
upon an object instance. In that case "self" will contain the Object
Instance you called it upon (which can be "nil") and not the class reference
This is not true for FPC (test it), and I can't imagine it would be true for
Delphi. Also see my other mail.
No matter whether you call a class method on a class or on an instance, 'self'
will always be a class, not an instance!
The following program
{$MODE OBJFPC}
type
TA = class
class procedure Fn;
end;
class procedure TA.Fn;
begin
if self=TA then writeln('self=TA');
end;
var
A:TA;
begin
TA.Fn;
A:=TA.Create;
A.Fn;
A.Destroy;
end.
outputs
self=TA
self=TA
Changing TA.Fn to
class procedure TA.Fn;
begin
if self=TA then writeln('self=TA');
if self is TA then writeln('self is TA');
end;
gives (correctly!) the compiler message
classmethods.pas(11,13) Error: class type expected, but got "Class Of TA"
classmethods.pas(11,13) Error: Incompatible type for arg no. 2: Got "Class Of TA",
expected "TObject"
Regards,
Bram
_________________________________________________________________
To unsubscribe: mail [EMAIL PROTECTED] with
"unsubscribe" as the Subject
archives at http://www.lazarus.freepascal.org/mailarchives