Abstract Methods... pop quiz
BlitzMax Forums/BlitzMax Programming/Abstract Methods... pop quiz
| ||
Okay... what's wrong with this, exactly ?SuperStrict Type SuperBlah Method New() DoIt() End Method Method DoIt() Abstract End Type Type SubBlah Extends SuperBlah Method DoIt() End Method End Type New SubBlah ...answers on a postcard to... :-/ |
| ||
You're calling an abstract method in the constructor of an abstract type (though not declared as abstract, it's abstract because of the abstract method). |
| ||
So, it's too early to call it? I guess that makes some kind of sense. Thanks :-) |
| ||
When you call a method in the New() method, the class' own method is called, not the one that would normally be used. |
| ||
While what you said makes sense, that really doesn't sound good at all. If you change the New() method to a different one and call it *after* the object is created, it knows what to do: SuperStrict Type SuperBlah Method DoThat() DoIt() End Method Method DoIt() Abstract End Type Type SubBlah Extends SuperBlah Method DoIt() End Method End Type Local a:SubBlah=New SubBlah a.DoThat 'Works, but OK, it's a SubBlah now, it knows what it is. SuperBlah(a).DoThat 'So it should think it's dealing with a SuperBlah, right? Wrong! This works too! So is there a really good reason why it can't do this in the New() method? Is it to save the compiler time rewriting function references for each extending type? |
| ||
So is there a really good reason why it can't do this in the New() method? Would be nice, and would certainly make my design cleaner... |
| ||
So is there a really good reason why it can't do this in the New() method? Surely the reason is because this is how inheritance works. When you create an object, it becomes a base class object first, then becomes an extended class object, and so on if there are multiple levels of inheritance. So in the New method of the base object, it cannot call the inherited method, because at this point in time, it is not of an inherited type. None of the inherited properties (fields and methods) have been added yet. And with delete, the same is true. The delete methods are called in reverse order, and so by the time it gets back to the base class, it is no longer inherited. Those inherited properties have been destroyed. EDIT: Furthermore, surely it *has* to work this way or properties in the base class would override properties in the extended class if they were added in a different order. |
| ||
That is massively interesting and answers my question entirely, thank you! So Brucey, I suppose the answer to your problem is to put whatever was in DoIt() inside the New() method itself for each extended type, or at least a call to DoIt() each time. |
| ||
suppose the answer to your problem is to put whatever was in DoIt() inside the New() method itself for each extended type Aye... that's what I decided to do - for now. Not too bad as there is only one subclass at the moment.. but ... my design!!!! *sniff* Oh well :-) Thanks to all for the insights... interesting reading as always. |
| ||
ultra geeky reading, but I like it. |