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

Just starting out? Need help? Post your questions and find answers here.
theteapot
User
User
Posts: 37
Joined: Fri Sep 09, 2005 7:46 am

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

Post 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)
Using PB 3.94 demo AND PROUD OF IT!!

*Goes back to little hole*
User avatar
fsw
Addict
Addict
Posts: 1603
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Post 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 
theteapot
User
User
Posts: 37
Joined: Fri Sep 09, 2005 7:46 am

Post by theteapot »

I don't have PB V4, so this comes up as a syntax error.

Surely there's some way of doing this!
Using PB 3.94 demo AND PROUD OF IT!!

*Goes back to little hole*
User avatar
fsw
Addict
Addict
Posts: 1603
Joined: Tue Apr 29, 2003 9:18 pm
Location: North by Northwest

Post 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)
theteapot
User
User
Posts: 37
Joined: Fri Sep 09, 2005 7:46 am

Post 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)
Using PB 3.94 demo AND PROUD OF IT!!

*Goes back to little hole*
User avatar
Maxus
User
User
Posts: 71
Joined: Thu Feb 16, 2006 9:35 am
Location: Russia
Contact:

Post 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
Post Reply