PDA

View Full Version : class of TMyObject



Robert Kosek
23-10-2007, 03:41 PM
I'm working to switch fully to FPC, and I need to know something I'm struggling to find in the documentation. In Delphi I can declare the following type:
type
TTypeOfMyStuff = class of TMyObject;

When I assign a variable this type I can point it at any descendant class, and from there even call a constructor and anonymously create that object. How do I do this in FPC? I want to be able to serialize my game, for saving it of course, and in doing so specify the types of objects and let them unserialize themselves. Of course I must first be able to recognize and point at the class itself.

Thanks in advance.

Mirage
23-10-2007, 04:03 PM
In FPC this work just as in Delphi.

Robert Kosek
23-10-2007, 04:33 PM
Okay, thanks. I couldn't find anything on it.

Does it work in OBJFPC mode and/or Delphi compatibility? And does the object/class difference cause any problems, to the best of your knowledge?

Ñuño Martínez
23-10-2007, 07:57 PM
From the FPC programmer's manual (Agoust 2006):

D.3 Delphi mode
This mode is selected by the $MODE DELPHI switch. It tries to emulate, as closely as possible, the behavior of Delphi 4. On the command-line, this mode is selected by the -Mdelpih switch.

1. You can not use the address operator to assign procedural variables.
2. A forward declaration must not be repeated exactly the same by the implementation of a function/procedure. In particular, you not omit the parameters when implementing the function or procedure.
3. Overloading of functions is not allowed.
4. Nested comments are not allowed.
5. The Objpas unit is loaded right after the system unit. One of the consequences of this is that the type Integer is redefined as Longint.
6. Parameters in class methods can have the same names as class properties (although it is bad programming practice).


D.5 OBJFPC mode
This mode is selected by the $MODE OBJFPC switch. On the command-line, this mode is selected by the -Mobjfpc switch.

1. You must use the address operator to assign procedural variables.
2. A forward declaration must be repeated exactly the same by the implementation of a function/procedure. In particular, you can not omit the parameters when implementing the function or procedure.
3. Overloading of functions is allowed.
4. Nested comments are allowed.
5. The Objpas unit is loaded right after the system unit. One of the consequences of this is that the type Integer is redefined as Longint.
6. You can use the cvar type.
7. PChars are converted to strings automatically.
8. Parameters in class methods cannot have the same names as class properties.

I never used the OBJECT style. I've read the documentation but I find it a bit confusing (the documentation not the OBJECT style) and I'm comfortable with CLASS.

Robert Kosek
23-10-2007, 09:00 PM
Which manual are you getting that from? I have the PDF documentation for FPC, but I can't seem to find the section you're referencing.

It seems that is referencing procedural variables, but I'm talking more about a "objective" variables. AKA, a pointer to a type. For instance all types descend from the base type, and the standard base constructor is called by a lookup table. That's pretty much what I'm after.

But I guess I could achieve something similar through a case statement and a class-type integer. :?

Ñuño Martínez
23-10-2007, 09:19 PM
You can read it on-line from here: http://www.freepascal.org/docs-html/

I downloaded all documentation from here: http://www.freepascal.org/down/docs/docs.var

I hope it helps you.

Robert Kosek
23-10-2007, 10:12 PM
Yes, I said it twice, but I have the documentation. Anyway, those sections are helpful with what mode restrictions there are, but not with this problem in particular.

Mirage
24-10-2007, 09:33 AM
I use the "class of ..." construction for serialization/deserialization as well and it works under Delphi and FPC. For FPC I used both DELPHI and OBJFPC modes. In OBJFPC DirectX 8 headers which I use won't compile, but my own code compiles. With DELPHI mode it compiles and runs.
"class of ..." is working only for classes not for objects.
Actually there are some strange bugs in my application compiled with FPC :( but serialization/deserialization works well.

marcov
28-10-2007, 11:11 AM
Class of works fine, I use it every day.

Ported Delphi software often is buggy on FPC/Lazarus, but usually they are simply bugs in the app that are not noticable on Delphi. It is similar with the move to Kylix in the Borland scene, where the same happened.

A _very_ important point for gamers , is that you should only draw in a components onpaint event, and never outside of it.