Inheritence and Global

BlitzMax Forums/BlitzMax Programming/Inheritence and Global

dan_upright(Posted 2013) [#1]
If I declare a type and then inherit that type, the inherited type has all the parent's fields, without having to declare them again. This I know. But what about Globals?

If I do this:
Type MyType
    Global Something
End Type

Type AnotherType Extends MyType

End Type

Does AnotherType have it's own global called Something, or does it share the same one as MyType? I'm wondering because I want to have a global list of all instances of a base type, including extended types and it'd be nice if I could use a TList within the base type for that.


UNZ(Posted 2013) [#2]
It is shared by all derivations. But you can have the same global name for another variable which can be confusing so be careful. In general I declare a global once in the first class it makes sense for.

example


ciao


dan_upright(Posted 2013) [#3]
Cheers mate.


dan_upright(Posted 2013) [#4]
Can anyone explain to me why this works the way it does?
Type basetype Abstract
	Field name:String
End Type

Type extendedtype Extends basetype
	Field name:String
End Type

bt:basetype = New extendedtype
bt.name = "Base"
extendedtype(bt).name = "Extended"
Print bt.name
Print extendedtype(bt).name

I wanted to set the name field in my extended types like this:
Field name:String = "Something"

I get that it works that way with globals but is there any real use to fields working this way?


Brucey(Posted 2013) [#5]
You can do this...
Type basetype Abstract
	Field name:String
End Type

Type extendedtype Extends basetype
	Method New()
		name = "Something"
	End Method
End Type



dan_upright(Posted 2013) [#6]
I'd forgotten about the New method, thanks for that.