Sign in Join

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

Mailing lists / fpc-pascal / 2003-08

A couple of easy questions on clsses

6 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

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

Aitor Santamaría Merino

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

Reply by email

Unknown author
to Aitor Santamaría Merino
Show quoted text 8 lines
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)?

Not yet in 1.0.10 . I believe it is implemented in 1.1

Show quoted text 5 lines
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.

This is correct.

Show quoted text 10 lines
(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).

Yes. I will check the wording.

Michael.

Reply by email

Aitor Santamaría Merino
to Unknown author
Show quoted text 7 lines
>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. > This is correct.

And what is the effect on a method? I suppose that any static method is a "class" method, right?

Aitor

Reply by email

Matt Emson
to Aitor Santamaría Merino: After reading the reference documentation on classes, I have a couple of easy aspects of…

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

Earlier message 36 lines
----- Original Message ----- From: "Aitor Santamaría Merino" To: "Free Pascal" 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 - > http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply by email

Michael Van Canneyt
to Aitor Santamaría Merino: And what is the effect on a method? I suppose that any static method is a "class" method,…
Show quoted text 11 lines
wrote: >>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. >> > >This is correct. > And what is the effect on a method? I suppose that any static method is a "class" method, right?

It depends what you mean with 'static'. In classes, the term 'static' has no defined meaning. You have 'normal' methods (where SELF is defined) and class methods (where SELF is not defined).

Michael.

Reply by email

Matt Emson
to Michael Van Canneyt
It depends what you mean with 'static'. In classes, the term 'static' has no defined meaning. You have 'normal' methods (where SELF is defined) and class methods (where SELF is not defined).

Note also that Class Methods kind of act as if they are external to the class - like tacked on procedures and functions. You can test this by using the following example:

type
  TMyTestClass = class
  public
    class procedure Test( AString: string ); //test calls
'writeln(AString);'
  end;

...

TMyTestClass.Test('Hello'); //no instance variable.. output is 'Hello' to
console

Hope that helps...

Matt

Reply by email