fb:porticula NoPaste
Virtuelle Funktionen in fb 0.24 ... 2. foo und Destructor virtuell
Uploader: | kawe |
Datum/Zeit: | 14.11.2012 16:15:36 |
Type Instance As Object
'--- base type, declaring a virtual destructor and a virtual sub "foo" ---
Type Upper Extends Object
Public:
Declare Sub novirt
Declare Destructor()
Protected:
fptr_destructor As Sub (As Instance)
fptr_foo As Sub (As Instance)
Declare Constructor()
Declare Sub foo
Declare Static Sub virt_destructor(As Instance)
Declare Static Sub virt_foo(As Instance)
End Type
Constructor Upper()
Print "called Upper::Constructor()"
this.fptr_destructor = @virt_destructor
this.fptr_foo = @virt_foo
'do something
End Constructor
Destructor Upper()
fptr_destructor(This)
End Destructor
Sub Upper.novirt
Print "called Upper::novirt()"
'do something
foo()
End Sub
Sub Upper.foo
fptr_foo(This)
End Sub
Sub Upper.virt_destructor(instance As Instance)
Print "called Upper::Destructor()"
'do something
End Sub
Sub Upper.virt_foo(instance As Instance)
Print "called Upper::foo()"
'do something
End Sub
'--- derived type, overwriting virtual destructor and sub "foo" ---
Type Middle Extends Upper
Public:
Declare Constructor()
Declare Sub other_novirt()
Protected:
Declare Static Sub virt_destructor(As Instance)
Declare Static Sub virt_foo(As Instance)
End Type
Constructor Middle()
Print "called Middle::Constructor()"
base.fptr_destructor = @virt_destructor
base.fptr_foo = @virt_foo
'do something
End Constructor
Sub Middle.other_novirt
Print "called Middle::other_novirt()"
'do something
End Sub
Sub Middle.virt_destructor(instance As Instance)
Print "called Middle::Destructor()"
'do something
Upper.virt_destructor(instance)
End Sub
Sub Middle.virt_foo(instance As Instance)
Print "called Middle::foo()"
'do something
Upper.virt_foo(instance)
Var pThis => Cptr(Middle Ptr, @instance)
pThis->other_novirt
End Sub
Type Lower Extends Middle
Public:
Declare Constructor()
Declare Sub other_novirt
Protected:
Declare Static Sub virt_destructor(As Instance)
Declare Static Sub virt_foo(As Instance)
End Type
'--- next derived type "Lower" ---
Constructor Lower()
Print "called Lower::Constructor()"
base.fptr_destructor = @virt_destructor
base.fptr_foo = @virt_foo
'do something
End Constructor
Sub Lower.other_novirt
Print "called Lower::other_novirt()"
'do something
base.other_novirt
End Sub
Sub Lower.virt_destructor(instance As Instance)
Print "called Lower::Destructor()"
'do something
Middle.virt_destructor(instance)
End Sub
Sub Lower.virt_foo(instance As Instance)
Print "called Lower::foo()"
'do something
Middle.virt_foo(instance)
End Sub
'--- demonstrate virtual sub and destructor calls
Dim As Upper Ptr m,l
m = New Middle()
Print"---"
m->novirt()
Print"---"
Delete m
Print "---"
l = New Lower()
Print"***"
l->novirt()
Print"***"
Delete l
Sleep