Recent

Author Topic: class variables and static fields  (Read 15133 times)

Zoran

  • Hero Member
  • *****
  • Posts: 1830
    • http://wiki.lazarus.freepascal.org/User:Zoran
class variables and static fields
« on: February 21, 2014, 03:59:04 pm »
Since FPC 2.6 class fields can be declared this way (as documented in this wiki page):
Code: [Select]
type
  TSomeClass = class
  public
    class var
      FSomeClassVar: TSomeType;
  end;

In this example variable FSomeClassVar is allocated globally (just one variable exists per class, not per class instance), and it can be accessed through class name (TSomeClass.FSomeClassVar).

In fpc documentation, I didn't find anything about these class variables.

However, in Reference guide, I found about static fields. Although Reference guide mentions these in chapter about old tp style objects, I tried and this syntax works in classes.

My question is - are there any differences between these two? Are these static fields and class vars actually same thing?

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: class variables and static fields
« Reply #1 on: February 21, 2014, 11:58:22 pm »
Quote
are there any differences between these two? Are these static fields and class vars actually same thing?
http://docwiki.embarcadero.com/RADStudio/XE5/en/Methods#Class_Static_Methods

Zoran

  • Hero Member
  • *****
  • Posts: 1830
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: class variables and static fields
« Reply #2 on: February 22, 2014, 12:34:46 am »
Quote
are there any differences between these two? Are these static fields and class vars actually same thing?
http://docwiki.embarcadero.com/RADStudio/XE5/en/Methods#Class_Static_Methods

Thanks, Leledumbo, for trying to help, but this link says nothing about what I asked. It doesn't even talk about fields, but about methods.
However, starting from your link, I jumped to the article about fields, where class fields in Delphi are mentioned. Still, there is nothing about static fields, so my question is still without answer.

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: class variables and static fields
« Reply #3 on: February 22, 2014, 02:36:49 am »
My question is - ... Are these static fields and class vars actually same thing?
Yes. Based on a comment in the source code, class var is a section where all fields inside it are static, while static directive is used for one single field.

Quote from: compiler\pdecvar.pas
    procedure read_record_fields(options:Tvar_dec_options; reorderlist: TFPObjectList; variantdesc : ppvariantrecdesc);
...
                 { if it is not a class var section and token=STATIC then it is a class field too }

Zoran

  • Hero Member
  • *****
  • Posts: 1830
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: class variables and static fields
« Reply #4 on: February 26, 2014, 08:51:53 am »
Thank you, so these are same.

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: class variables and static fields
« Reply #5 on: February 26, 2014, 10:00:12 am »
@Zoran/anyone: please feel free to raise a bug against documentation/fpdoc on the bugtracker pointing out the missing documentation. Doc bugs are almost always resolved, though it may take some time...
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

Zoran

  • Hero Member
  • *****
  • Posts: 1830
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: class variables and static fields
« Reply #6 on: February 26, 2014, 11:35:57 am »
@Zoran/anyone: please feel free to raise a bug against documentation/fpdoc on the bugtracker pointing out the missing documentation. Doc bugs are almost always resolved, though it may take some time...

Okay, here it is: http://bugs.freepascal.org/view.php?id=25786

BigChimp

  • Hero Member
  • *****
  • Posts: 5740
  • Add to the wiki - it's free ;)
    • FPCUp, PaperTiger scanning and other open source projects
Re: class variables and static fields
« Reply #7 on: February 26, 2014, 11:51:19 am »
Thanks a lot!
Want quicker answers to your questions? Read http://wiki.lazarus.freepascal.org/Lazarus_Faq#What_is_the_correct_way_to_ask_questions_in_the_forum.3F

Open source including papertiger OCR/PDF scanning:
https://bitbucket.org/reiniero

Lazarus trunk+FPC trunk x86, Windows x64 unless otherwise specified

 

TinyPortal © 2005-2018