mik*_*bal 16 c++ virtual inheritance class function
我有一些像这样的事件
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
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法强制调用onLoad而不实际编写Father :: onLoad()?
哈希解决方案是受欢迎的:)
Oli*_*rth 27
如果我理解正确,你需要它,以便每当调用overriden函数时,必须始终首先调用基类实现.在这种情况下,您可以调查模板模式.就像是:
class Base
{
public:
void foo() {
baseStuff();
derivedStuff();
}
protected:
virtual void derivedStuff() = 0;
private:
void baseStuff() { ... }
};
class Derived : public Base {
protected:
virtual void derivedStuff() {
// This will always get called after baseStuff()
...
}
};
Run Code Online (Sandbox Code Playgroud)