Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2012-08

class of a class function

5 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

CA Gorski

How can I get the actual class a class function is called in?

TSomething = class
     class function GetMyClass: TClass;
end;
TOtherthing = class(TSomething)
end;

...

class function TSomething.GetMyClass: TClass;
begin
     Result:=???;
end;

...

var
     MyVar: TClass;
MyVar:=TOtherthing.GetMyClass; // MyVar should be set to TOtherthing

Reply by email

Jonas Maebe
to CA Gorski

CA Gorski wrote on Tue, 07 Aug 2012:

I tried that already, but at least in my case Self is Nil inside the class function. Compiler (fpc 2.6.1 ) bug?

It does not return nil for me:

***
{$mode objfpc}
type
   TSomething = class
     class function GetMyClass: TClass;
   end;
class function TSomething.GetMyClass: TClass;
begin
   Result:=self;
end;
begin
   writeln(ptrint(TSomething.GetMyClass));
end.
***

Jonas

Reply by email

CA Gorski
to Jonas Maebe

Yes, you are right.
Due to the problem that I couldn't store a class function in a var of type function of object (I reported earlier) I found a workaround:

var
     MyVar: function: boolean of object;
...
     (nil).MyClassFunc;

and that's why Self is nil. But I can do

     (TSomeClass).MyClassFunc;

which solves all problems (so far). :)

Jonas Maebe wrote 33 lines
CA Gorski wrote on Tue, 07 Aug 2012: > I tried that already, but at least in my case Self is Nil inside the > class function. > Compiler (fpc 2.6.1 ) bug? It does not return nil for me: *** {$mode objfpc} type TSomething = class class function GetMyClass: TClass; end; class function TSomething.GetMyClass: TClass; begin Result:=self; end; begin writeln(ptrint(TSomething.GetMyClass)); end. *** Jonas _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email