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

Anton Tichawa anton.tichawa at chello.at
Sat Jul 10 01:57:34 CEST 2004


Ron Weidner wrote:

>I thought I was getting closer to understanding
>pointers but it appears that I'm still not getting it.
> I wrote the following code fragment but it doesn't
>compile.  the errors I get are...
>Widget_container_class.pas(80,27) Error: Illegal
>Widget_container_class.pas(88,23) Error: Illegal
>Widget_container_class.pas(88,29) Error: Illegal
>Widget_container_class.pas(88,29) Fatal: Syntax error,
>";" expected but "(" found
>I numbered lines 80 and 88 in comments so that the
>error messages make sense.  (to the experienced
>programmer like yourself :) )Please let me know if you
>see what the problems are.  
>unit Widget_container_class;
>type PWidgetList=^TWidgetList;
>	widget:pointer;
>	next:PWidgetList;
>type PWidgetContainer=^TWidgetContainer;
>	private 
>		PCurWidget,PHeadWidget:PWidgetList;
>		text:string;
>	public	
>		constructor init(ATag:string);	
>		procedure Render();
>		procedure SetText(str:string);
>		function GetText():string;	
>		procedure MoveFirstWidget();
>		procedure MoveLastWidget();
>		procedure AddWidget(PWidget:pointer);
>		procedure RenderWidgets();
>procedure TWidgetContainer.RenderWidgets();
>	MoveFirstWidget();
>	while (PCurWidget^.next <> nil) do
>	begin
>		PCurWidget^.widget:=PWidget(PCurWidget^.widget); //
>cast the pointer to a widget pointer
>		if (PCurWidget^.widget^.meta = 'ENTITY') then //
>this is line 80
>		begin
>		end
>		else
>		begin
>		end;
>		PCurWidget^.widget^.Render(); //line 88
>		PCurWidget:=PCurWidget^.next;
>		dispose(PHeadWidget);
>		PHeadWidget:=PCurWidget;		
>	end;
>Do you Yahoo!?
>New and Improved Yahoo! Mail - Send 10MB messages!
>fpc-pascal maillist  -  fpc-pascal at lists.freepascal.org
The type cast actually does nothing at runtime, so the line

        PCurWidget^.widget:=PWidget(PCurWidget^.widget); //

does nothing.

Type casts are just to inform the compiler, at compile time, to accept a 
type for some variable or constant it would otherwise reject.

So, you have to typecast where you use it, i. e. -everywhere- you use it.

Instead of:

        PCurWidget^.widget:=PWidget(PCurWidget^.widget); //
        if (PCurWidget^.widget^.meta = 'ENTITY') then //


        if (PWidget(PCurWidget^.widget)^.meta = 'ENTITY') then //

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.



More information about the fpc-pascal mailing list