Inheritance bug?
BlitzMax Forums/BlitzMax Programming/Inheritance bug?
| ||
When I run this code: It outputs: Start GxtkApp - Update I would have thought it would output: Start AppDevice - Update Bug?! |
| ||
Interesting! Maybe the vmt is not fully setup when you hit the ctor of GxtkApp? What happens if you make GxtkApp::Update() abstract? |
| ||
Well you would be wrong in your thinking (Although understandably). Update is called at the level of GxtApp, so its update on that level which is called. That is it isn't an inherited object calling New, it is the base object Oh and give better class names next time, like one two three or similar |
| ||
In Java: This prints: Start AppDevice - Update ActionScript does the same... its only BlitzMax which runs the GxtkApp update... BTW this is the Monkey generated code... |
| ||
In some languages, such as C++, calling a function in a constructor always calls the function assosiated with THAT class's constructor, even if the function is overridden in a derived class, and you have created a new instance of that class. In other languages such as Java, it always calls the function assosioated with the actual type you are creating, even in a base constructor. I think C# also does the same. This is a bit suprising - I thought Blitz would follow the latter, but it seems it's the former. So this isn't so much a bug as it is a gotcha. You can get around it by creating your types from create functions and call the functions you want from them. |
| ||
You can get around it by creating your types from create functions and call the functions you want from them. Yep, that's what I ended up doing, but it meant changing the Mojo's app.monkey file, which i did not want to do :( |
| ||
I didn't know monkey could generate Blitz code. |
| ||
Perhaps this is relevant: http://www.blitzbasic.com/Community/posts.php?topic=66144#739775 Old, but it does address this topic. |
| ||
I didn't know monkey could generate Blitz code. That's therevills baby. He is actually creating the parser and translator(or maybe just the translator). I think that's pretty neat. Last edited 2011 |
| ||
Thanks for the link Yasha. I'm sure I've read Java and maybe even C# don't follow the same behavior as C++ on this rule. Sometimes it can be useful for the functions called in the constructor to be the derived type, but if the function uses fields which aren't set up yet it can be dangerous. It's tricky in C++ as you use the constructors a lot, and it would be nice to be able to choose whether to call the function virtually or not in the constructor. In Blitz it's less of a problem. It's very common to use some kind of create function, which does its stuff after the object has been new-ed already. Blitz's new isn't as flexible as a C++ constructor (no parameters etc) so I think it's relied upon less than in C++ style. I'm a big fan of OO and use it in all my projects, but I have to say the code above is an example of OO gone mad in my opinion. Impossible to follow! Last edited 2011 |
| ||
That's cool therevills! How are you solving problems of features available in monkey but not in BMax? Such as constructor parameters? |