Super?
BlitzMax Forums/BlitzMax Beginners Area/Super?
| ||
Super Super evaluates to Self cast to the method's immediate base class. What? Hehe, I really, really don't get that :-) |
| ||
EDIT: Didn't explain it too well, lets try again: Supposing we had a type Animal and a type which extends Animal called Cat. If they both implemented different Die methods, then calling Super.Die from inside one of Cat's methods actually calls Animal.Die because Cat is a type of Animal. Calling Self.Die or just Die on the other hand from one of Cat's methods would call Cat.Die Type Animal Method Die() Print "An animal died." End Method End Type Type Cat Extends Animal Method Die() Print "A cat died." End Method Method WaysToKillACat() 'Prints "A cat died" (same as just Die on its own) Self.Die 'Prints "An animal died" Super.Die End Method End Type |
| ||
So it's like referencing an extended type's parent? If so, why is it called Super? |
| ||
Yes. I don't know why it is called super, I personally think the term Base would be better, but it doesn't really matter. |
| ||
Hi Robert, Good example! Thank you. "super" doesn't sound "Blitz" too me, what is does and how it is named isn't logical to me. Robert's example should appear in the docs! |
| ||
Super is Javaesque, Base is C++ esque - Blitz is more Java like in it's other keyword uses (Final, Abstract) too. |
| ||
Also, I tend to think along the lines of... Type X End Type Type Y Extends X End Type Type Z Extends Y End Type X and Y are both base types of Z. However, only Y is Z's super type - ie: 'immediate' base type. |
| ||
Ahh, interesting differentiation. So can you get to a non immediate base with Blitzmax? Aaron |
| ||
Just tried it...and no:Type X Method T() Print "X.T" End Method End Type Type Y Extends X Method T() Print "Y.T" End Method End Type Type Z Extends Y Method T() Super.Super.T Print "Z.T" End Method End Type Local t:Z=New Z t.T I probably could get this to work - but I'm not sure if it's a good idea style-wise! |
| ||
Could you maybe do it by having a GetSuper method in each type that returns access to it's super? |
| ||
Well you could argue that only the most recent derived class should call the super, but howabout a class reference call instead. So using your example you'd go Type Z Extends Y Method T() X.T <- state X explicitly Print "Z.T" End Method End Type |
| ||
I probably could get this to work - but I'm not sure if it's a good idea style-wise! No. Use composite pattern instead. |
| ||
What is the purpose of the abstract keyword after the method in the example above? The docs says that it means this method has no implementation and it must be implemented by a derived type. So wouldn't Super.Die cause an error or does it work because it is called from a derived type? |
| ||
If I understand correctly, while you must override it, it still has a default implementation - yes, so you get base (Super) class functionality still, even if you really need to derive from the class as a whole. |
| ||
If I understand correctly, while you must override it, it still has a default implementatio No - abstract methods do not have an implementation. That code will not compile. |
| ||
Ahh OK Thanks. I hadn't tried it, and assumed that that code was compiling and therefore worked like C++. Aaron |
| ||
Ahh OK Thanks. I hadn't tried it, and assumed that that code was compiling and therefore worked like C++. In C++, pure virtual functions are empty and must be implemented by the derived class. |
| ||
Actually, in C++ you *can* provide an implementation for a pure virtual function. |
| ||
Unless we're talking about different things, that's incorrect. Virtual functions are optional, but a pure virtual function has to be overloaded in the derived class or the compiler will throw a fit. virtual MyFunction(); vs virtual MyFunction() = 0; |
| ||
I'm with Warren on this one... HAS to be overridden by the class that inherits the base class. |
| ||
Not wanting to nit pick, but Mark said implementation not instantiation. |
| ||
Actually, in C++ you *can* provide an implementation for a pure virtual function. http://www.parashift.com/c++-faq-lite/abcs.html#faq-22.4 Take that! :p |