Forum > General

class var problem?

(1/2) > >>

apexcol:
I have this...

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---TFruit = class    class var FhowMany: integer;  private    class procedure SetHowMany(const aValue: integer); static;  public    class constructor create;    class destructor destroy;    class property amount: integer read FhowMany write SetHowMany;  end;     { ... } var  FruitsBasket: TList;  class procedure TFruit.SetHowMany(const aValue: integer);begin  if FhowMany <> aValue then    FhowMany := aValue;end; class constructor TFruit.create;begin  inherited;  inc(FhowMany);end; class destructor TFruit.destroy;begin  dec(FhowMany);  inherited;end; var  i: integer;  F: TFruit;begin  FruitsBasket := TList.Create;   for i:=1 to 10 do     FruitsBasket(TFruits.Create);   writeln('Fruits: ' + F.amount);end. Question:  Why printed result is only 1?  it ought to be 10, right?

Kays:
Uh, the notion constructor/destructors only exist for objects, instances of a class. So a “class constructor”/“class destructor” is kinda wrong.

Maybe, I usually put classes into separate units and utilize the initialization and finalization block in the unit for operation on the class, maybe a “class constructor” behaves like doing stuff in such an initialization section, especially since you're in a program (I see your last line reads end dot).

PS: Writing class and the static modifier is redundant.
PPS: Your class var FHowMany is in the default section, which public, are you aware of that?

Thaddy:

--- Quote from: Kays on December 31, 2017, 06:12:08 am ---Uh, the notion constructor/destructors only exist for objects, instances of a class.

--- End quote ---
Actutally,with trunk this is possible because of management operators. I will add an example;

molly:

--- Quote from: Kays on December 31, 2017, 06:12:08 am ---Uh, the notion constructor/destructors only exist for objects, instances of a class. So a “class constructor”/“class destructor” is kinda wrong.

--- End quote ---
Uhm... unless i misunderstood your words then no, it is not kinda wrong.

See Delphi documentation:

--- Quote ---A class constructor is a special class method that is not accessible to developers. Calls to class constructors are inserted automatically by the compiler into the initialization section of the unit where the class is defined. Normally, class constructors are used to initialize the static fields of the class or to perform a type of initialization, which is required before the class or any class instance can function properly. Even though the same result can be obtained by placing class initialization code into the initialization section, class constructors have the benefit of helping the compiler decide which classes should be included into the final binary file and which should be removed from it.

--- End quote ---

I've seen some talk on that subject with regards to Free Pascal but have no idea if this already works for Free Pascal (without any issues)

edit: ah Thaddy beat me to it   ;D

Thaddy:
I removed the interffaced example, here's another, simpler and cleaner one, more like you first proposed:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{$ifdef fpc}{$mode delphi}{$H+}{$I-}{$endif}uses fgl;type TFruit = classstrict private  class constructor create; //yes, molly this workspublic  class var Count:Longint;  constructor create;  destructor destroy;override;end; class constructor TFruit.create;begin  Count := 0;end; constructor TFruit.Create;begin  inherited;  inc(Count);end; destructor TFruit.Destroy;begin  dec(Count);  inherited;end; var    Basket:TFPGObjectlist<TFruit>;  i:Integer;begin  Basket := TFPGObjectList<Tfruit>.Create;  Basket.FreeObjects := true;  for i := 0 to 9 do  begin    Basket.Add(TFruit.Create);    writeln(Basket[i].count);  end;  Basket.Free;end..
I think this is what you need: not overly complex and very clean code.

Navigation

[0] Message Index

[#] Next page

Go to full version