Sign in Join

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

Mailing lists / fpc-pascal / 2012-08

virtual class var?

6 messages · 2 people ·

Reply by email Subscribe to this list Your subscription

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

Andrew Haines

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

Paul Ishenin
to Andrew Haines: Hi, is it possible to have a virtual class variable? I want to have a pointer available…
Andrew Haines wrote 11 lines
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

Andrew Haines
to Paul Ishenin
Paul Ishenin wrote 9 lines
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

Paul Ishenin
to Andrew Haines
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

Andrew Haines
to Paul Ishenin

Ahhh ok I understand now. , Thank you.

Sent from my Android phone with K-9 Mail. Please excuse my brevity. Paul Ishenin 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