Sign in Join

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

Mailing lists / fpc-pascal / 2011-02

Classes and class methods

8 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

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

Mark Morgan Lloyd

Is it possible to put a class (not an instance) in a variable or array element, and to use this to call a class method?

I'm defining a hierarchy of classes and it would be convenient if it were possible to register the fact that a class had a particular subclass, such that the information was available before instantiation.

I'm assuming that the code that inspects the array has imported all units so is aware of the relevant class declarations, but I am still having problems with calling class methods and/or casting to a class (as distinct from an instance).

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]

Reply by email

Marco van de Voort
to Mark Morgan Lloyd

In our previous episode, Mark Morgan Lloyd said:

Show quoted text 11 lines
Is it possible to put a class (not an instance) in a variable or array element, and to use this to call a class method? I'm defining a hierarchy of classes and it would be convenient if it were possible to register the fact that a class had a particular subclass, such that the information was available before instantiation. I'm assuming that the code that inspects the array has imported all units so is aware of the relevant class declarations, but I am still having problems with calling class methods and/or casting to a class (as distinct from an instance).

Afaik this route you can only call static methods of the basetype of the class-type.

So if it is

     A= class
          end;
     b=class(a)
          end;
     c=class(b);
     TClasstype  = class of  B;
var x : TClasstype;
begin
  x:=C;
  x.staticmethod

then staticmethod must be visible in B. It doesn't matter which descendant of B you assign to X.

This is logical, since static methods are entirely compiletime, and the compiler can't know what type will be runtime assigned.

Reply by email

Mark Morgan Lloyd
to Marco van de Voort
Marco van de Voort wrote 39 lines
In our previous episode, Mark Morgan Lloyd said: > Is it possible to put a class (not an instance) in a variable or array > element, and to use this to call a class method? > > I'm defining a hierarchy of classes and it would be convenient if it > were possible to register the fact that a class had a particular > subclass, such that the information was available before instantiation. > > I'm assuming that the code that inspects the array has imported all > units so is aware of the relevant class declarations, but I am still > having problems with calling class methods and/or casting to a class (as > distinct from an instance). Afaik this route you can only call static methods of the basetype of the class-type. So if it is A= class end; b=class(a) end; c=class(b); TClasstype = class of B; var x : TClasstype; begin x:=C; x.staticmethod then staticmethod must be visible in B. It doesn't matter which descendant of B you assign to X. This is logical, since static methods are entirely compiletime, and the compiler can't know what type will be runtime assigned.

Thanks Marco, I was beginning to suspect that I was hitting limitations and that what I was trying to do might be ill-advised.

I'm finding virtual class methods useful when I'm able to refer directly to a class, rather than going via a reference to a class.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]

Reply by email

Mark Morgan Lloyd
to Mark Morgan Lloyd
Mark Morgan Lloyd wrote 45 lines
Marco van de Voort wrote: > In our previous episode, Mark Morgan Lloyd said: >> Is it possible to put a class (not an instance) in a variable or >> array element, and to use this to call a class method? >> >> I'm defining a hierarchy of classes and it would be convenient if it >> were possible to register the fact that a class had a particular >> subclass, such that the information was available before instantiation. >> >> I'm assuming that the code that inspects the array has imported all >> units so is aware of the relevant class declarations, but I am still >> having problems with calling class methods and/or casting to a class >> (as distinct from an instance). > > Afaik this route you can only call static methods of the basetype of the > class-type. > > So if it is A= class > end; > b=class(a) > end; > c=class(b); > > TClasstype = class of B; > > > var x : TClasstype; > > begin > x:=C; > > x.staticmethod > > then staticmethod must be visible in B. It doesn't matter which > descendant > of B you assign to X. > > This is logical, since static methods are entirely compiletime, and the > compiler can't know what type will be runtime assigned. Thanks Marco, I was beginning to suspect that I was hitting limitations and that what I was trying to do might be ill-advised. I'm finding virtual class methods useful when I'm able to refer directly to a class, rather than going via a reference to a class.

I'm doing something wrong- it works.

I've got a sequence of classes, each with a Parent class procedure and Children() class function which reference a global array associated with each class (hardcoded name). Each class also has an Oid() function which appends a hardcoded number to whatever it's inherited.

If a test program does this

WriteLn(OidToStr(TIso.Oid));
WriteLn(OidToStr(TIso.Children[0].Oid));
WriteLn(OidToStr(TIso.Children[0].Children[0].Oid));
WriteLn(OidToStr(TIso.Children[0].Children[0].Children[0].Oid));

I get this

.1
.1.3
.1.3.6
.1.3.6.1

i.e. each reference to Children() is identifying the correct method, despite the fact that this is spread over more than one file.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]

Reply by email

Jonas Maebe
to Mark Morgan Lloyd
Mark Morgan Lloyd wrote 7 lines
I'm doing something wrong- it works. I've got a sequence of classes, each with a Parent class procedure and Children() class function which reference a global array associated with each class (hardcoded name). Each class also has an Oid() function which appends a hardcoded number to whatever it's inherited.

A "class reference", which is the official name of a "class of xxx" type, is basically a fancy monicker for the VMT of a class. So it's normal that calling virtual class methods via class references works as you'd expect.

Jonas

Reply by email

Mark Morgan Lloyd
to Jonas Maebe
Jonas Maebe wrote 13 lines
On 09 Feb 2011, at 13:41, Mark Morgan Lloyd wrote: > I'm doing something wrong- it works. > > I've got a sequence of classes, each with a Parent class procedure and > Children() class function which reference a global array associated > with each class (hardcoded name). Each class also has an Oid() > function which appends a hardcoded number to whatever it's inherited. A "class reference", which is the official name of a "class of xxx" type, is basically a fancy monicker for the VMT of a class. So it's normal that calling virtual class methods via class references works as you'd expect.

It was the "class of" bit that I was initially missing- and I think I used the term "reference" in my initial question because I was gradually getting my head around the various issues.

In actual fact I was looking at the Forward type declarations section in the Reference guide yesterday and trying to work out exactly what "only possible with pointer types and classes" meant- pointers are adequately covered but I wonder whether an example of the latter form would be appropriate, even though it precedes the chapter on classes?

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]

Reply by email

Luiz Americo Pereira Camara
to Mark Morgan Lloyd: Is it possible to put a class (not an instance) in a variable or array element, and to…
Mark Morgan Lloyd wrote 12 lines
Is it possible to put a class (not an instance) in a variable or array element, and to use this to call a class method? I'm defining a hierarchy of classes and it would be convenient if it were possible to register the fact that a class had a particular subclass, such that the information was available before instantiation. I'm assuming that the code that inspects the array has imported all units so is aware of the relevant class declarations, but I am still having problems with calling class methods and/or casting to a class (as distinct from an instance).

Are you looking for something like this?:
http://code.google.com/p/luipack/source/browse/trunk/luicontrols/wizardcontrols.pas#266

Luiz

Reply by email

Mark Morgan Lloyd
to Luiz Americo Pereira Camara
Luiz Americo Pereira Camara wrote 16 lines
On 9/2/2011 07:58, Mark Morgan Lloyd wrote: > Is it possible to put a class (not an instance) in a variable or array > element, and to use this to call a class method? > > I'm defining a hierarchy of classes and it would be convenient if it > were possible to register the fact that a class had a particular > subclass, such that the information was available before instantiation. > > I'm assuming that the code that inspects the array has imported all > units so is aware of the relevant class declarations, but I am still > having problems with calling class methods and/or casting to a class > (as distinct from an instance). > Are you looking for something like this?: http://code.google.com/p/luipack/source/browse/trunk/luicontrols/wizardcontrols.pas#266

No, I'm doing something simpler- setting up a sequence of classes to mimic the definition of a node in an SNMP MIB file. I've got to the point where I can query (the Pascal transcription of) a MIB file to find out what nodes it implements and can work backwards and forwards to list the OIDs associated with a node and its ancestors-descendants- all without instantiating anything.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]

Reply by email