Constructors and Inheritance
BlitzMax Forums/BlitzMax Beginners Area/Constructors and Inheritance
| ||
Blitzmax does not allow me to have one constructor and cast it down to its child.Strict Type Father Field Name:String Function create:Father(s:String) Local f:Father=New Father f.Name=s Return f End Function Method display() Print "Name:"+Name+" Type:Father" End Method End Type Type son Extends Father Method display() Print "Name:"+Name+" Type:Son" End Method End Type Type daughter Extends Father Method display() Print "Name:"+Name+" Type:Daughter" End Method End Type Local Man:Father=Father.Create("John") Man.Display() Local Boy:Son=Son(Father.Create("Jack")) Boy.Display() Local Girl:Daughter=Daughter(Father.Create("Jill")) Girl.Display() End or am I doing something wrong? |
| ||
You can't cast down the inheritance tree, only up (from son to father, daughter to father). What you would want to use is: Strict Type Father Field Name:String Function create:Father(s:String) Local f:Father=New Father f.Name=s Return f End Function Method display() Print "Name:"+Name+" Type:Father" End Method End Type Type son Extends Father Function create:Father(s:String) Local t:Son=New Son t.Name = s Return Father(t) End Function Method display() Print "Name:"+Name+" Type:Son" End Method End Type Type daughter Extends Father Function create:Father(s:String) Local t:daughter=New daughter t.Name = s Return Father(t) End Function Method display() Print "Name:"+Name+" Type:Daughter" End Method End Type Local Man:Father=Father.Create("John") Man.Display() Local Boy:Father=Son.Create("Jack") Boy.Display() Local Girl:Father=daughter.Create("Jill") Girl.Display() End Reason: Son is extended from Father, so Son shares the properties with his father. Casting no problem. But Father does not share Son properties, so casting this way is not possible. But as you see above, the extended types method is called if you call display on something that was casted from an extended type to father. |
| ||
Interesting. Thanks a bunch |
| ||
You surely can downcast the hierarchy, you just have to do it more explicit because by downcasting your objects may or may not be of the required types. Local Man:Father = new Son Local Boy:Son = Son(Man) Now Boy holds a reference to a Son instance Local Girl:Daughter = Son(Man) Now Girl is Null because Man is only a Father and a Son but not a Daughter. |
| ||
Thats what I said, not? You can cast from Son to Father (and that than back), but you can't cast a Father to Son if it was not a Son originally. |
| ||
Yeah you're right - just wanted to clarify that you can work with base classes and cast them down to their more concrete types with explicit casts if they are really those kinds of objects - whereas you can always be sure an upcast to the parent classes is always possible. I thought what the original poster maybe wants to do is f.i. managing a list of entities and have a few special cases where he has to check the concrete type and only act on some of the possible derivations. But yes, we can't cast a reference to anything the referenced object instance isn't at any stage of its typehierarchy. |