Inheritence and Global
BlitzMax Forums/BlitzMax Programming/Inheritence and Global
| ||
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. |
| ||
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 |
| ||
Cheers mate. |
| ||
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? |
| ||
You can do this...Type basetype Abstract Field name:String End Type Type extendedtype Extends basetype Method New() name = "Something" End Method End Type |
| ||
I'd forgotten about the New method, thanks for that. |