Troubles with types

BlitzMax Forums/BlitzMax Beginners Area/Troubles with types

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

1) If you would use:
Local pet:Animal = New Animal
Local pet:Animal = New Animal


Then the address of the first animal is overwritten by the address of the second animal.
That leaves the first animal instance unreferenced (no variable points to it anymore) and will be deleted by the next FlushMem.

2) I'm not sure about this, but I think you are actually declaring a new Animal object, which is extended by the Cat object's fields.
I think that's why the Name-field from the Animal type is returned.
Personally I think that you should never have 2 fields with the same name, that's just asking for confusion.

3) The benefit is that you can use that same variable with several different types:
pet:Animal = New Animal
pet:Animal = New Cat
pet:Animal = New Dog
pet:Animal = New Fish

You can also check if an object is of a certain type:
If Dog(pet) then Print "This object is of type Dog"
If Cat(pet) then Print "This object is a fish"


The casting ("Dog(pet)" for example) returns the address of the object if the pet is actually a dog and returns "0" if the pet-variable points to another type (a fish for example).
So the If statement is True (any other value than "0") if the pet-variable holds a pointer to a Dog-object.

Also you can use that same approach for passing any animal to a function:
Function ProcessAnimal(pet:Animal)
	Select True
		Case Dog(pet)
			...
		Case Fish(pet)
			...
		Case Cat(pet)
			...
		Case Animal(pet)
			...
	End Select

	...
End Function

Now you can pass any type of animal (an animal object or any of the derived types of an animal), check to see which kind it really is (by casting) and processing it, using a select case statement for example and just doing what needs to be done per type of animal.