The ability to Create non-visual Classes

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Re: The ability to Create non-visual Classes

Post by Trond »

PMV wrote:
Trond wrote:Interfaces make it difficult to allow instances to override methods.
The vtable points to a list of pointers to the procedurs. If you want to change the
call of one procedure, you only need to override this single pointer. That is the same,
if you want to override the pointer of a prototype.

MFG PMV
Normally you would place the vtable in the datasection, and it will be shared by all instances. Overriding a method for all instances is still simple, but overriding a method for just one instance is not.
PMV
Enthusiast
Enthusiast
Posts: 727
Joined: Sat Feb 24, 2007 3:15 pm
Location: Germany

Re: The ability to Create non-visual Classes

Post by PMV »

i never have used a vTable pointing to a DataSection :lol:
I have always created a constructor like this:

Code: Select all

Interface MyClass
  Shout(Msg.s)
EndInterface

Structure MyClass_Struc
  vTable.i
  *Shout
  
  Name.s
EndStructure


Procedure MyClass_Shout(*Self.MyClass_Struc, Msg.s)
  MessageRequester("Shout!", Msg + " from " + *Self\Name + "!")
EndProcedure

Procedure MyClass(Name.s)
  Protected *Self.MyClass_Struc = AllocateMemory(SizeOf(MyClass_Struc))
  InitializeStructure(*Self, MyClass_Struc)
  *Self\vTable = *Self + SizeOf(INTEGER)
  *Self\Shout = @MyClass_Shout()
  *Self\Name = Name
  ProcedureReturn *Self
EndProcedure


Bill.MyClass = MyClass("Bill")
Bob.MyClass = MyClass("Bob")

Bill\Shout("Hello world!")
Bob\Shout("Goodbye, world!")
Looks like your example with Prototypes, but a little bit better.
:D

MFG PMV
Post Reply