[fpc-pascal] How to make class constructor body calls descendant's overriden method/property?

JC Chu jcchu at acm.org
Sat Nov 17 16:42:43 CET 2012


A CLASS CONSTRUCTOR is a special CLASS STATIC method executed during
unit initialization.  Because it is CLASS STATIC, Self is undefined
(and thus (1) what you write for ClassName in TghModel.Create really
stands for TghModel.ClassName, and (2) if TUsers inherits it, the same
code will be executed).

What you want amounts to getting the list of classes with a common
ancestor.  To my knowledge is no way to do that automatically.

On Sat, Nov 17, 2012 at 10:48 PM, leledumbo <leledumbo_cool at yahoo.co.id> wrote:
> Cross posted from stackoverflow:
> http://stackoverflow.com/questions/13431079/how-to-make-class-constructor-body-calls-descendants-overriden-method-property
>
> I'm writing an ORM framework and got stuck in a way to automatically
> determine table name from class name. In my base object for the ORM to work,
> I have:
>
> TghModel = class
> ...
>   class var FTableName: String;
>   class constructor Create;
> ...
>
> whose implementation is:
>
> class constructor TghModel.Create;
> begin
>   FTableName := ClassName;
>   Delete(FTableName,1,1); // Delete 'T'
> end;
>
> My assumption was that ClassName method will return the real class name.
> e.g. if I have:
>
> TUsers = class(TghModel)
>
> then FTableName will be initialized to TUsers instead of TghModel, which is
> wrong. I want to avoid users to make class constructor for each classes
> inheriting from TghModel, especially because the content would be totally
> the same as in TghModel.Create. Is there any way to implement it?
>
>
>
> --
> View this message in context: http://free-pascal-general.1045716.n5.nabble.com/How-to-make-class-constructor-body-calls-descendant-s-overriden-method-property-tp5711994.html
> Sent from the Free Pascal - General mailing list archive at Nabble.com.
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal



-- 
Best regards,
JC Chu



More information about the fpc-pascal mailing list