But when I run it the last call on Person\Display() causes a runtime error of invalid memory address? I am able to call the method via CallFunctionFast with the function pointer though?
DeclareModule Person
EnableExplicit
Interface iPerson
SetName(Name.s)
GetName.s()
SetAge(Age)
GetAge()
Display()
Destroy()
EndInterface
Declare.i CreatePerson(Name.s)
EndDeclareModule
Module Person
Structure obj
vTable.i ; virtual table must be the first entry !!
Name.s
Age.i
EndStructure
; -- Methods
; =============================================================================
Procedure.i SetName(*this.obj, Name.s)
If *this
*this\Name = Name
EndIf
EndProcedure
Procedure.s GetName(*this.obj)
Protected Result.s = ""
If *this
Result = *this\Name
EndIf
ProcedureReturn Result
EndProcedure
Procedure.i SetAge(*this.obj, Age)
If *this
*this\Age = Age
EndIf
EndProcedure
Procedure.i GetAge(*this.obj)
Protected Result.i
If *this
Result = *this\Age
EndIf
ProcedureReturn Result
EndProcedure
Procedure.i Display(*this.obj)
If *this
Debug "Person Data"
Debug "-----------"
Debug "Name : " + *this\Name
Debug "Age : " + *this\Age
Debug "==========="
EndIf
EndProcedure
Procedure.i DestroyObject(*this.obj)
FreeStructure(*this) : *this = #Null
ProcedureReturn *this
EndProcedure
; -- Public
Procedure.i CreatePerson(Name.s)
Protected *this.obj = AllocateStructure(obj)
If *this
*this\vTable = ?vTable_iPerson
*this\Name = Name
EndIf
ProcedureReturn *this
EndProcedure
; -- Virtual Table
; =============================================================================
; the order of the Methods must be the same like in Interface-EndInterface !
DataSection
vTable_iPerson:
Data.i @SetName()
Data.i @GetName()
Data.i @SetAge()
Data.i @GetAge()
Data.i @Display()
Data.i @DestroyObject()
EndDataSection
EndModule
UseModule Person
; Demo
Person1.iPerson = CreatePerson("Devox")
Person1\SetAge(20)
Person1\Display()
Person1\Destroy()
In your example you can only use Methods without parameters... and Callfunction... you should not use it, because it is very error-prone !
Prototype is the keyword for search in PB-Manual, if you want to use CallFunction or CallFunctionFast ...
PureBasic 6.10 LTS (Windows x86/x64) | Windows10 Pro x64 | Asus TUF X570 Gaming Plus | R9 5900X | 64GB RAM | GeForce RTX 3080 TI iChill X4 | HAF XF Evo | build by vannicom
English is not my native language... (I often use DeepL to translate my texts.)
The examples from mk-soft helped me a lot... Thanks go to him
PureBasic 6.10 LTS (Windows x86/x64) | Windows10 Pro x64 | Asus TUF X570 Gaming Plus | R9 5900X | 64GB RAM | GeForce RTX 3080 TI iChill X4 | HAF XF Evo | build by vannicom
English is not my native language... (I often use DeepL to translate my texts.)
It's a very old sets of tutorials. That being said if you want to really experiment some OOP with PureBasic I suggest to download, study and compile for your self Dev-Object that can write code like that in matter of milliseconds. For example :
StarBootics wrote: ↑Sat Apr 24, 2021 2:40 am
It's a very old sets of tutorials. That being said if you want to really experiment some OOP with PureBasic I suggest to download, study and compile for your self Dev-Object that can write code like that in matter of milliseconds.
I had a feeling what I was doing maybe out of date, found that problem a few times whilst learning PB. Your tool looks just brilliant and really helpful with this I will be sure to give it a try.
If you then start with external interfaces (Windows COM Objects), that the parameter passing and just the return of the result of a method looks a little different.
The result of a method is then always whether the method was successfully executed and for the return value a pointer must be passed in the parameter.
If you are interested, you can also have a look at OOP-BaseClass and, if you are ready, at OOP-BaseClassDispatch for creating DLLs externally.