The most people here could know memory protective V-Tables in DataSections.
I want to show you another way.
Code: Select all
; PureBasic-Lounge.de
; Date: 31. October 2007
; OS: Windows, Linux, Mac
; Demo: Yes
Interface IcMyClass
Set (sText.s)
Get.s ()
EndInterface
Structure cMyClass
*VTABLE
sText.s
EndStructure
Procedure.l cMyClass_Set (*This.cMyCLass , sText.s)
*this\sText = sText
ProcedureReturn #True
EndProcedure
Procedure.s cMyClass_Get (*This.cMyCLass)
ProcedureReturn *this\sText
EndProcedure
Procedure CreateObject_cMyClass ()
Protected *This.cMyClass = AllocateMemory (SizeOf (cMyClass))
Static *cMyClass_Vtable ; <--- Static pointer to our V-Table
If Not *This
ProcedureReturn #Null
EndIf
; Generation a V-Table for this class, if not already exist.
If Not *cMyClass_Vtable
*cMyClass_Vtable = AllocateMemory (SizeOf(IcMyClass))
If Not *cMyClass_Vtable
FreeMemory (*This)
ProcedureReturn #Null
EndIf
; Adding methodes
; Use PokeL and the interface offsets. Its a easy way to do that.
PokeL (*cMyClass_Vtable + OffsetOf (IcMyClass\Set ()) , @cMyClass_Set ())
PokeL (*cMyClass_Vtable + OffsetOf (IcMyClass\Get ()) , @cMyClass_Get ())
EndIf
; announce the address of the V-Table for our new object.
*This\VTABLE = *cMyClass_Vtable
ProcedureReturn *This
EndProcedure
;--- Test
*MyObj.IcMyClass = CreateObject_cMyClass ()
*MyObj\Set ("Hello World")
Debug *MyObj\Get ()
Best regards
Wolf
PS: Sorry for my funny english.
