Version 2 (modified by landauf, 8 years ago) (diff) |
---|
HowTo: Super
Description
SUPER is a macro defined in core/Super.h. It's capable to call a function in the parent class of another class. This avoids class-hierarchy-dependend code like MyParentClass::function();. Instead we use SUPER(MyClass, function);. As you can see, the super-call depends only on the current class. If the parent changes, we don't have to modify the code.
Important: A super-function must be virtual and public. If you don't declare the function public you'll get a compiler error complaining about the private nature of the function.
doc/Super explains in detail how to add a new super-function.
Usage
This is how you call the SUPER macro:
- SUPER(MyClass, functionname, arguments…);
MyClass is the name of the class implementing this function.
functionname is the name of the super-function.
arguments: The arguments passed to the function. If the function takes no arguments, SUPER gets called with only 2 parameters (MyClass and functionname).
Example
This is an example how to use SUPER:
*.h file:
#include "core/BaseObject.h" class MyClass : public BaseObject { public: virtual void XMLPort(Element& xmlelement, XMLPort::Mode mode); };
*.cc file:
void MyClass::XMLPort(Element& xmlelement, XMLPort::Mode mode) { SUPER(MyClass, XMLPort, xmlelement, mode); doSomething(); ... }