Question on usage of Virtual

General FreeBASIC programming questions.
Post Reply
wallyg
Posts: 270
Joined: May 08, 2009 7:08
Location: Tucson Arizona

Question on usage of Virtual

Post by wallyg »

Can someone explain why this is not working the way I expect from what I read in the documentation

Code: Select all

Type Display Extends Object
	as Display Ptr		ObjectType

	Declare virtual sub redraw
End Type
Private sub Display.redraw
...
end sub

Type Container Extends Display	
	Declare sub redraw
end type
Private sub Container.redraw
...
end sub

Type Frame Extends Container
	Declare sub redraw
end type
Private sub Frame.redraw
...
end sub

' main program

dim as display ptr P = New Frame

p->redraw
	
I actually have dozens of first level extend types of Display. Each of them have dozens of second level extends. Up to 7 levels deep. The pointers to the objects created all have as the first variable the type of object created. And all pointers to these objects are stored as a display level pointer.

Since the object P that was created was a Frame, I expected that Frame.redraw would be executed.

According to FBdebugger Container.redraw was called instead

Why and what can I do to make the version of redraw executed to match the actual type created. I thought that was what the virtual routine definition of redraw would do.
fxm
Moderator
Posts: 12132
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Question on usage of Virtual

Post by fxm »

In a multi-level inheritance, all the procedures to be overridden must be declared as virtual at each hierarchy level:
(specificity of the FB language)

Code: Select all

Type Container Extends Display   
   Declare virtual sub redraw
end type
See documentation about the "Virtual" keyword and this sentence:
The property of being a virtual method is not implicitly inherited by the overriding method in the derived type.

If you used the "Override" attribut in the "frame.redraw()" declaration, the compiler would return an error ("error 222: Not overriding any virtual method").
wallyg
Posts: 270
Joined: May 08, 2009 7:08
Location: Tucson Arizona

Re: Question on usage of Virtual

Post by wallyg »

Thank you for your advice. I will place the Virtual keyword in each Declare of the routine redraw.
fxm
Moderator
Posts: 12132
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Question on usage of Virtual

Post by fxm »

KeyPgVirtual → fxm [Added a precision on chaining overriding methods]
wallyg
Posts: 270
Joined: May 08, 2009 7:08
Location: Tucson Arizona

Re: Question on usage of Virtual

Post by wallyg »

Thank you. I guess my documentation was quite out of date. Never noticed that fact.

So if I read that correctly, if in my above example where P is a pointer to Display (bottom most level), if I want to delete the Frame (top level) that is pointed to by it, I have to put Virtual on each of the Destructors in all of the types in this structure and then it will execute the Destructors in the order Frame, then Container, then Display and finally the system Type OBJECT.
fxm
Moderator
Posts: 12132
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Question on usage of Virtual

Post by fxm »

Exactly.

Remark on the wording:
Generally, ''top level' corresponds to the base type (Oject) and 'bottom level' to the most derived type (Frame), like the order you write the Type declarations.
Post Reply