[fpc-pascal]A couple of easy questions on clsses

Matt Emson memsom at interalpha.co.uk
Fri Aug 15 11:18:09 CEST 2003


Written in Delphi, so use Delphi compatibility mode when testing in fpc....

program TestStatic;

uses sysutils;

type
  Test = class
  public
    class function TestValue( x: integer = -1): integer; //unless you pass a
value, returns static value. If pass value, returns new value.
  end;

{ Test }

class function Test.TestValue(x: integer): integer;
const
  v: integer = 0;
begin
  if x <> -1 then
    v := x;

  result := v;
end;

var
  a, b, c: Test;

begin
  a := Test.Create;
  b := Test.Create;
  c := Test.Create;

  writeln('a ', a.testvalue); // 'a 0'

  writeln('b = 10 ', b.testvalue(10)); // 'b=10 10'

  writeln('c ', c.testvalue); //'c 10'

  writeln('Test.TestValue := 25 ', Test.testvalue(25) ); // 'TestValue := 25
25'

  writeln('a ', a.testvalue); // 'a 25'

  writeln('b ', b.testvalue); 'b 25'

  writeln('c ', c.testvalue); 'c 25'

  readln; //pause (for running under windows...
end.


AFAIK, that's as close as you can get without additional language support...
You may need FPC 1.1 if 1.0 doesn't support assignable typed constants.

Hope that helps,

Matt


----- Original Message -----
From: "Aitor SantamarĂ­a Merino" <aitor.sm at wanadoo.es>
To: "Free Pascal" <fpc-pascal at lists.freepascal.org>
Sent: Thursday, August 14, 2003 12:19 PM
Subject: [fpc-pascal]A couple of easy questions on clsses


> Hi,
>
> After reading the reference documentation on classes, I have a couple of
> easy aspects of the documentation that were unclear to me.
>
> (1) Is there the concept of "static field" or "class field" (meaning a
> field that is not instanciated, but remains the same var for all the
> instances)? I know this can be easily made with an external variable,
> but I consider it to be a different concept.
> The syntax diagrams of classes show that a method can be preceded by the
> keyword "class" (and I failed to find where it is explained what for?),
> but does not seem to admit this keyword before a field.
>
> (2) In the documentation, it is said "The Inherited keyword will not
> jump to the inherited method, if virtual was used." and also "The
> compiler will compile it, but using Inherited can produce strange
> effects." (where neither of the examples shows the word "Inherited"),
> but I fail to understand if these sentences refer to the case in which
> you do it wrong, and declare both MyProc as virtual (instead as
> "virtual" and "override", respectively).
> So should I understand that I can use Inherited without problems if I do
> it right? (means virtual/override).
>
> Aitor
>
>
> _______________________________________________
> fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
> http://lists.freepascal.org/mailman/listinfo/fpc-pascal





More information about the fpc-pascal mailing list