English 中文(简体)
Help me with a solution for what could be solutioned by virtual static fields... in FPC
原标题:

I m doing an event manager in Freepascal
Each event is an object type TEvent (=object), each kind of event must derive from this class.
Events are differentiated by an integer identificator, assigned dynamically.
The problem is that i want to retrieve the event id of an instance, and i can t do it well.

  • All instances of a class(object) have a unique id => so it should be static field.
  • All classes have a diferent id => so it should be virtual.
  • Event ids are assignated in run time, and can change => so it can t be a simple method

In sum, I can t put all this together.
I m looking for an elegant solution, i don t want to write a hardcoded table, actualizing it in every constructor... etc, i d prefer something taking advantage of the polymorphism
Can anyone help me with another technical or design solution?
I remark I don t want to use class instead of object construct.(property doesn t work on objects? :(

问题回答

You might need class var like stuff, like in newer delphi s. But that is in the development version of FPC only (2.5.1+).

Note that the object type is TP legacy, and hasn t been developed on in this millenium, and I don t expect that to change. If you need more than it offers, I suggest to use classes.

You can make a simple table/list like this:

unit classids;

{$mode objfpc}{$H+}

interface

function GetClassID(c:TClass):Integer;
procedure SetClassID(c:TClass; id:Integer);

property ClassID[c:TClass]:Integer read GetClassID write SetClassID;

implementation
uses Maps;

var Map:TMap;

function GetClassID(c:TClass):Integer;
begin
 if not Map.GetData(c,Result) then
  Result:=0; //Or any default you like
end;

procedure SetClassID(c:TClass; id:Integer);
begin
 Map.Delete(c);
 Map.Add(c,id);
end;

initialization
 Map:=TMap.Create(itu4,SizeOf(Integer));
finalization
 FreeAndNil(Map);
end.

Then you can get/set the id with

ClassID[TMyObject]:=12;
ShowMessage(IntToStr(ClassID[TMyObject])); //shows 12

Good luck





相关问题
Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

JSON with classes?

Is there a standardized way to store classes in JSON, and then converting them back into classes again from a string? For example, I might have an array of objects of type Questions. I d like to ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Passing another class amongst instances

I was wondering what is the best practice re. passing (another class) amongst two instances of the same class (lets call this Primary ). So, essentially in the constructor for the first, i can ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签