Extends calls New()

BlitzMax Forums/BlitzMax Beginners Area/Extends calls New()

Jeremy Alessi(Posted 2004) [#1]
If one type extends another and I have a Create function then the New() function of the super type is automatically called by the compiler correct?

According to the Rockout code this looks to be true ... but I'm getting an error apparently in my own create function that I'm attempting to access a field or method of a null object ...


Bot Builder(Posted 2004) [#2]
I should think that the compiler would not even have two objects, and the fields of the base object, and it's bases (if any) would be simply added into the Derived object.


BlitzSupport(Posted 2004) [#3]
I think we'll need to see some code... any New () method you create is called when you do x:Blah = New Blah. If an extended type has no New method, that of the super/parent type will be called, if there is one.


FlameDuck(Posted 2004) [#4]
Post some code. What you're saying seems to be accurate.


Bot Builder(Posted 2004) [#5]
Really? Hrmm. Sounds inefficient to me.... I suppose you have to have a way to reference a composite object as the base type. You could store that the type can be referenced as other types, guaranteing that the new composite type will have the same fields and methods.


FlameDuck(Posted 2004) [#6]
Hrmm. Sounds inefficient to me....
How? His wording is innaccurate (it's actually the New command that, as part of creating an object, calls the New method). Just like in Java.


Jeremy Alessi(Posted 2004) [#7]
Yeah Extends calls new is kinda incorrect ... I just wanted to make sure that if an extended type didn't have a new() function that it's base type's new function was automatically called ... all this because DrawImage(x, y, image) .... <runs and hides>


PowerPC603(Posted 2004) [#8]
If you create a new extended (derived) type, both New() methods are executed (the one of the Derived type AND the one of the Base type):

Type BaseType
	Method New()
		Print "The basetype's New()-method is executed"
	End Method
End Type

Type DerivedType Extends BaseType
	Method New()
		Print "The derived type's New()-method is executed"
	End Method
End Type

' These both options are valid
test:DerivedType = New DerivedType
test2:BaseType = New DerivedType


All other "super"-type's New()-methods are executed as you can see here:
Type BaseType
	Method New()
		Print "The basetype's New()-method is executed"
	End Method
End Type

Type DerivedType Extends BaseType
	Method New()
		Print "The derived type's New()-method is executed"
	End Method
End Type

Type DerivedType2 Extends DerivedType
	Method New()
		Print "The second derived type's New(--method is executed"
	End Method
End Type

test:DerivedType2 = New DerivedType2