Post Instantiation Inheritance?
BlitzMax Forums/BlitzMax Programming/Post Instantiation Inheritance?
| ||
I'd like to instantiate an extension of a base class after the base class had already been instantiated, for example,Type Base Field Name:String End Type Type Extension Extends Base Field ExtensionName:String End Type b:Base = New Base e:Extension = Extension(b) e.ExtensionName = "TestExtension" Print e.ExtensionName I want to do this because I want to be able to "Extend" the actions of the base class without "modifying" the initial base class. If you have an alternative approach I'm very interested to hear. Thanks Last edited 2011 |
| ||
Is this what you want :-Type Base Field Name:String End Type Type Extension Extends Base Field ExtensionName:String End Type b:Base = New Base e:Extension = New Extension e.ExtensionName = "TestExtension" Print e.ExtensionName Last edited 2011 |
| ||
No, Lets say for example I have code that works exactly the way I want it with the base class, so I package it into a module. Now I don't want to modify that module at all because it's perfect, but I do want to add behavior. |
| ||
But that codes does that no?Type Base Field Name:String End Type Type Extension Extends Base Field ExtensionName:String End Type b:Base = New Base e:Extension = New Extension e.Name = "Test" e.ExtensionName = "TestExtension" Print e.Name Print e.ExtensionName |
| ||
In that code you can access the base classes functionality by instantiating the extension class, but what if you already have an instance of the Base class and now you want to convert it to the extension class, I want to "upgrade" my INSTANCE of my base class to one of its extensions. Does that make sense? |
| ||
the reason your example won't work is because a base class does not know how to convert itself into the extended class. That is not how extended and casting work. when you create a base type object, only the space for the base object is allocated. When you create an extended type object, the space for both the base and the extended object is allocated. when you create an extend class you can assign it to a base type variable but that will only give you access to the base type part of the extended class ( sort of, its a little bit more complicated than that) and that is why you can cast an extended class back and forth and not the pure instance of a base class. You can't simply just cast it. It won't work! the only thing I can suggest you do is what col said. you can create a base or a extended and assign them accordingly! and is what programmers usually do when interested in extending functionality. |
| ||
Ok, understood, Just looking for a more elegant way to extend without modifying code. |
| ||
maybe it will work for you if you create a type that has both types as fields and instantiate the field variables depending on your needs. |
| ||
How about this :-Type Base Field Name:String End Type Type Extension Extends Base Field ExtensionName:String Method Create:Base() Return Self EndMethod End Type Local b:Base = New Extension.Create() b.Name = "Base" Local eb:Extension = Extension(b) eb.ExtensionName = "Extension" Print eb.Name Print eb.ExtensionName EDIT :- You can create a means in the inherited type to return itself as the base type, then you can cast back up with :- Print b.Name Print Extension(b).ExtensionName Its still not like a complete 'lift' into the inherited type because it means creating from the inherited to receive the base and then having to use a 'up cast', but it will function the same. Last edited 2011 |