Page 1 of 1

OOP in purebasic (my simple test doesn't work)

Posted: Sat Mar 18, 2006 6:48 am
by theteapot
I'm trying out OOP in purebasic, and I put together this from Drac's oop tutorial, but I can't get it to work.

It gives out an "Invalid memory access", which would seem to me as if the object isn't being initialized properly. However, the structure matches up perfectly with the interface.

Can someone help?

Code: Select all

Interface My_Object
 Procedure1(x.l, y.l)
EndInterface

Structure My_Methods
 *Procedure1.l
EndStructure

Procedure My_Procedure1(x.l, y.l)
 Debug "Yay!"
EndProcedure

Methods.My_Methods
Methods\Procedure1 = @My_Procedure1()

Object.My_Object = @Methods

Object\Procedure1(10, 20)

Posted: Sat Mar 18, 2006 7:25 am
by fsw
Try this:

Code: Select all

Interface My_Object
  Procedure1(x.l, y.l)
EndInterface

Structure My_Methods
  *VirtualTable.l
EndStructure

Procedure My_Procedure1(x.l, y.l)
  Debug "Yay!"
EndProcedure

Methods.My_Methods
Methods\VirtualTable = ?VirtualMethods

Object.My_Object = @Methods

Object\Procedure1(10, 20)

DataSection
  VirtualMethods:
  Data.l @My_Procedure1()
EndDataSection 

Posted: Sat Mar 18, 2006 7:46 am
by theteapot
I don't have PB V4, so this comes up as a syntax error.

Surely there's some way of doing this!

Posted: Sat Mar 18, 2006 4:27 pm
by fsw
Sorry, thought everybody moved on...
Here for older versions:

Code: Select all

Interface My_Object
  Procedure1(x.l, y.l)
EndInterface

Structure My_Methods
  *VirtualTable.l
EndStructure

Structure Methods
  My_Proc1.l
EndStructure 

Procedure My_Procedure1(x.l, y.l)
  Debug "Yay!"
EndProcedure

Virtual.Methods
Virtual\My_Proc1 = @My_Procedure1()

Methods.My_Methods
Methods\VirtualTable = @Virtual

Object.My_Object = @Methods

Object\Procedure1(10, 20)

Posted: Sat Mar 18, 2006 9:15 pm
by theteapot
Thanks a lot for that!

I've simplified it to remove a structure, but it may not work when you add in more objects. However, for a one-object program someone might find it usefull.

Code: Select all

Interface My_Object
  Procedure1(x.l, y.l)
EndInterface

Structure Methods
  My_Procedure1.l
EndStructure

Procedure My_Procedure1(x.l, y.l)
  Debug "Yay!"
EndProcedure

Virtual.Methods
Virtual\My_Procedure1 = @My_Procedure1()

*VirtualTable.l = @Virtual

Object.My_Object = @*VirtualTable

Object\Procedure1(10, 20)

Posted: Mon Mar 20, 2006 9:29 am
by Maxus
theteapot wrote:Thanks a lot for that!

I've simplified it to remove a structure, but it may not work when you add in more objects. However, for a one-object program someone might find it usefull.

Code: Select all

Interface My_Object
  Procedure1(x.l, y.l)
EndInterface

Structure Methods
  My_Procedure1.l
EndStructure

Procedure My_Procedure1(x.l, y.l)
  Debug "Yay!"
EndProcedure

Virtual.Methods
Virtual\My_Procedure1 = @My_Procedure1()

*VirtualTable.l = @Virtual

Object.My_Object = @*VirtualTable

Object\Procedure1(10, 20)
Here there is a discrepancy:

Code: Select all

Procedure My_Procedure1(x.l, y.l)
   Debug x
   Debug "Yay!"
EndProcedure
Correct your code:

Code: Select all

Procedure My_Procedure1(*Safe, x.l, y.l)
  Debug x
  Debug "Yay!"
EndProcedure
Otherwise it will not read parameter X.l