[fpc-pascal] virtual class var?
Hi, is it possible to have a virtual class variable? I want to have a pointer available per class that can be a different value. Something like this: TA = class class var Foo: TObject; virtual; end; TB = class(TA) class var Foo: TSpecialObject; override; end; I have in mind multiple levels of inheritance. I am storing a vmt like record/object in the variable, so the variable the child class overrides will be compatible with the ancestor class. It's possible there may be many instances of the objects so I do not want to create an variable per instance if I can help it. Regards, Andrew ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] virtual class var?
My current idea for a solution is here: http://pastebin.com/P3JsDQ03 Can anyone think of something with less code? I guess I could save a little if I skip the property and directly use GetVMT and SetVMT. Regards, Andrew ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] virtual class var?
07.08.12, 8:18, Andrew Haines wrote: Hi, is it possible to have a virtual class variable? I want to have a pointer available per class that can be a different value. Something like this: TA = class class var Foo: TObject; virtual; end; TB = class(TA) class var Foo: TSpecialObject; override; end; Class variable is stored the same way as a regular variable and has the only difference is that it can be accessible with the class name prefix. The thing you need requires different implementation - something like storing a virtual class variable in VMT. There is no implementation for that. Best regards, Paul Ishenin ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] virtual class var?
On 08/06/12 23:02, Paul Ishenin wrote: Class variable is stored the same way as a regular variable and has the only difference is that it can be accessible with the class name prefix. The thing you need requires different implementation - something like storing a virtual class variable in VMT. There is no implementation for that. Best regards, Paul Ishenin What is the current implementation? I would guess that class vars are stored in the vmt already... Please correct me if I've got this wrong. Current classes have a structure containing partially the following: TClassInstance = record vmt: Pointer; {Space for parent class variables are stored here followed by variables declared for this object} variable1: SomeType; variable2: SomeType; etc. end; TClassVMT = record parent_class_vmt: Pointer; virtual_procs: array[0..n] of pointer; class_var_1: Sometype; class_var_2: Sometype; etc... end; Is this right? Thanks, Andrew ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] virtual class var?
07.08.12, 11:24, Andrew Haines wrote: What is the current implementation? I would guess that class vars are stored in the vmt already... No, class var and regular variable has no difference except the scope. It is a static variable which is shared between all instances and descendants - so why should it be stored in vmt or near it? Best regards, Paul Ishenin ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] virtual class var?
Ahhh ok I understand now. , Thank you. -- Sent from my Android phone with K-9 Mail. Please excuse my brevity. Paul Ishenin paul.ishe...@gmail.com wrote: 07.08.12, 11:24, Andrew Haines wrote: What is the current implementation? I would guess that class vars are stored in the vmt already... No, class var and regular variable has no difference except the scope. It is a static variable which is shared between all instances and descendants - so why should it be stored in vmt or near it? Best regards, Paul Ishenin _ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org http://lists.freepascal.org/mailman/listinfo/fpc-pascal