I have some events like this
class Granpa // this would not be changed, as its in a dll and not written by me
{
public:
virtual void onLoad(){}
}
class Father :public Granpa // my modification on Granpa
{
public:
virtual void onLoad()
{
// do important stuff
}
}
class Child :public Father// client will derive Father
{
virtual void onLoad()
{
// Father::onLoad(); // i'm trying do this without client explicitly writing the call
// clients code
}
}
Is there a way to force calling onLoad without actually writing Father::onLoad()?
Hackish solutions are welcome :)
As suggested above but applied to your case.
However, nothing can prevent Child to override onLoad since c++ has no final keyword and Granpa onLoad is itself virtual.
If I understand correctly, you want it so that whenever the overriden function gets called, the base-class implementation must always get called first. In which case, you could investigate the template pattern. Something like:
If the object in question is small and cheap to copy, simply call the base class' copy constructor, i.e. BaseClass(*derived_class_object).method()
Well, there is no widely known, recognized way to do this. If there had been, GUI libraries using events and signals would probably have implemented it. However there are other solutions to your problem.
You could implement a signal - event connection, using boost.Signals, sigslot or something of your own making. As GTK+ does:
In a less C-centric way, this could be implemented along these lines: