Extending TPixmap derived type problems
BlitzMax Forums/BlitzMax Programming/Extending TPixmap derived type problems
| ||
I am trying to extend TPixmap, but the inheritance doesn't seem to entirely work. Example: SuperStrict Type TPixmapDraw Extends TPixmap Method calc() Print "calc" EndMethod EndType Print "start" Local pix:TPixmapDraw = New TPixmapDraw pix = LoadPixmap("test1.jpg") ''why doesn't this work pix.calc Print pix.width End the error I get is unable to convert (type) to (type) But yet the BlitzMax manual states that the derived type should be compatible where the base type is expected. Any suggestions? Am I missing something or is this not allowed in BlitzMax? Many thanks. |
| ||
The built-in BlitzMax modules are not designed to be extended in this way. LoadPixmap() returns TPixmap... the instance is created elsewhere. A TPixmap is not a TPixmapDraw, hence the error. A TPixmapDraw is a TPixmap though... |
| ||
Thanks for the insight. But I still don't see the error, unless it's just a restricted thing and I need to accept it. The code: pix = LoadPixmap("test1.jpg") is: TPixmapDraw = TPixmap Can't a TPixmapDraw be assigned a TPixmap? (derived assigned a base, since the derived contains the base) |
| ||
Thanks for the insight. But I still don't see the error, unless it's just a restricted thing and I need to accept it. It's not a "restricted thing" and I don't know any programming language which would allow you to do what you think you're doing. is: TPixmapDraw = TPixmap Absolutely correct. That's exactly what it's doing. Can't a TPixmapDraw be assigned a TPixmap? No. How could it possibly? You're creating a TPixmap and then telling the compiler that it's really a TPixmapDraw. But you didn't create a TPixmapDraw, you created a TPixmap. The constructor (the New method) allocated memory and sets up the function table for your object, and it gives your object everything a TPixmap should have, and nothing more. So that Calc method you created was not placed in the object's function table when it was created because you didn't ask BlitzMax to create a TPixmapDraw. So when you come to call that function that doesn't exist, what do you think BlitzMax is going to do? Crash badly is my guess of what would happen. The compiler tries to prevent that by not allowing you to make the invalid typecast in the first instance. It works in reverse because accessing less than everything that was created is perfectly possible, but you must surely see that accessing more than was created is not possible. |
| ||
The built-in BlitzMax modules are not designed to be extended in this way. true but that shouldn't stop anyone from doing whatever they want with them as long as they are not releasing any of the Bmax code as their own. aside from the code possibly becoming obsolete when Mark desides to make changes to a/the module, I would say to look at the modules source code before attempting something like this. Work based on what you know not what you ass-u-me. Easy for me to say. As if I hadn't fallen in to the same pitfall before. ;) |
| ||
It works in reverse because accessing less than everything that was created is perfectly possible, but you must surely see that accessing more than was created is not possible. Ahhhhh.... that makes sense, thank you for the explanation. |