Extended Objects

BlitzMax Forums/BlitzMax Beginners Area/Extended Objects

Patch(Posted 2009) [#1]
Given the code below. I want to be able to pass an instance of TBar into the create method of TBaz (placeholder obj). However I may have other classes that also inherit from TBar as well. Obviously if the create method expects the type TFoo then I wouldn't have access to the methods on TBar. Likewise if the create method expects TBar then any other object that extends TFoo would not have its methods accessible either.


Type TFoo
  'super class
End Type

Type TBar Extends TFoo
 Method test()
 End Method
End Type

Type TBaz
 Method Create(obj)
 End Method
End Type


I am completely new to Blitzmax so this may be pretty easy and I'm just completely missing it somehow.

Any ideas?


Kurator(Posted 2009) [#2]
SuperStrict

Type TFoo
  'super class
End Type

Type TBar Extends TFoo
 Method test()
 End Method
End Type

Type TBaz
 Method Create(obj:TFoo)
 End Method
End Type


Local bar:TBar = New TBar
Local baz:TBaz = New TBaz

baz.Create(bar)



Patch(Posted 2009) [#3]
Thanks for that Kurator. I had to add an empty method of the same name to the superclass. It then calls the method of the same name on the correct instance, seems similar to a virtual function. However, does this mean that I would have to add an empty method of the same name to the superclass everytime? Surely not?


Kurator(Posted 2009) [#4]
if you want to call a specifc mehtod on the bar Object this mehtod has to exist in the complete TFoo type hierachy.

There are two ways how you can handle this:

Either your TFoo Type has an valid Implementation of the Mehtod you want to call from a TBaz Type (like bar.test() ) - or you declare test() as an Abstract Method in TFoo - so that every child of TFoo has to provide an implementation of this Method.


Gabriel(Posted 2009) [#5]
Since you seem to be familiar with OO programming in other languages, a Blitzmax Abstract method is the equivalent of a pure virtual function.


Patch(Posted 2009) [#6]
Thats helpful to know. Thank you :)