[fpc-pascal]Linked List/ pointers/ casting/ OOP Question

Anton Tichawa anton.tichawa at chello.at
Sat Jul 10 19:28:00 CEST 2004


Hello, Ron!

Ron Weidner wrote:

>>Note: I don't see the definition of PWidget in your
>>code peace. I assume 
>>it's a pointer to a record containing a string
>>member named meta. But in 
>>your procedure
>>
>> procedure AddWidget(PWidget:pointer);
>>
>>you use the very same identifier, PWidget, for a
>>variable name. That's a 
>>bit confusing.
>>    
>>
>
>You are the 2nd person that I've talked to that seemed
>confused about this.  Since I'm very new to pascal
>programming, I'm concerned that I may have a bad
>design.  In an attempt to confirm a design problem I'm
>going to try to explain what I'm trying to do and ask
>for advice on a better way to do it.
>
>Ok... Basically I have 3 objects that look like
>this...
>
>              Widget
> ---------------|----------
>|                          |
>Container Widget    Entity Widget
>
>One of the defining differances between an Entity
>Widget and a Container widget is that a Container
>Widget can contain other widgets.  (both other
>container widgets and entity widgets).  So I needed to
>create a dynamic list (linked list) that could address
>either a container widget or an entity widget.  So I
>created TWidgetList with the field 'widget' being an
>untyped pointer.  Now that I have this list, I need to
>add addresses of _existing_ widgets to the list.  So,
>when I call the method...
>
>procedure AddWidget(PWidget:pointer); 
>
>I'm referring to a PWidget but I don't know of which
>type.  (container or entity) 
>
>During construction of an Entity or Container widget,
>the string meta is set to identify what type of widget
>it is.  Now later, I can traverse the TWidgetList,
>read the meta string, cast the widget pointer to the
>right type and call the correct object methods. 
>(Render() in this case.)
>
>Is my code the long way around the barn or does it
>seem to make better sense explained?
>
>Ron_W
>  
>  
>
Your design is the right one for the problem, -and- FreePascal supports 
this design very well. What you seem to miss is the correspondence 
between your design ideas and FreePascal's syntax and semantics.

Sorry, but before proceeding I ask you to make a decision (because else 
writing any code examples might be wasting our time):

FreePascal offers two different approaches to your design: Objects and 
Classes. Objects is what you're using now. Classes are essentially 
pointers to Objects, but they also simplify the syntax, and they allow 
pointer types to inherit from each other. This latter capability 
perfectly addresses the problem of choosing the right type for the 
parameter X in AddWidget(X). So, before proceeding, please decide if you 
want to continue using good old Objects, or if you want to switch to the 
newer and more powerful Classes. If unsure, choose Objects. Also, if you 
cannot easily change the declaration of a widget because it's legacy 
software, choose Objects. I'll then post code examples using the 
approach you choose.

Anton.







More information about the fpc-pascal mailing list