如何强制孩子使用相同的虚函数首先调用其父虚函数 [英] How to force child same virtual function call its parent virtual function first
问题描述
guys,
我有一个case需要子类需要调用其父虚拟函数之前调用其重写虚函数。
guys, I have a case that needs the child class needs to call its parent virtual function at first before call its override virtual function.
BaseClass::Draw()
{
}
ChildClass::Draw()
{
BaseClass::Draw(); // BaseClass Draw must be called first.
}
GrandChildClass::Draw()
{
ChildClass::Draw(); // ChildClass Draw must be called first.
}
我想从客户端隐藏此行为。是否有模式?
I want to hide this behavior from clients. Is there pattern on this?
感谢。
推荐答案
您可以使用第二个私有成员函数作为可覆盖的行为:
For simple cases you can use a second, private member function for the overrideable behavior:
class Base {
public:
void Draw() {
// Base-class functionality here
OverrideableDraw();
}
private:
virtual void OverrideableDraw() { }
};
class Derived : public Base {
private:
virtual void OverrideableDraw() {
// Derived-class functionality here
}
};
对于更复杂的层次结构这是不可能的,因为任何派生类都可以覆盖任何虚拟成员函数(在C ++中没有 final
)。通常可以安全地假设每个派生类正在做正确的事情。虽然我可以想到几次,我遇到了问题,因为派生类拧紧了重写,这些情况通常是很容易调试。
For more complex hierarchies (e.g. where you have multiple levels of inheritance), this isn't possible since any derived class can override any virtual member function (there is no final
in C++). Usually it is safe to assume that each derived class is doing the right thing. While I can think of a few times that I've run into issues because a derived class screwed up overriding, those cases were usually pretty straightforward to debug.
如果你是真的担心它,真的想保证基类覆盖首先执行,你可以使用像这样,虽然这是相当昂贵的(至少这个天真的实现是相当昂贵的):
If you are really worried about it and really want to guarantee that base-class overrides are executed first, you could use something like this, though this is quite expensive (at least this naive implementation is quite expensive):
#include <functional>
#include <iostream>
#include <vector>
class Base {
public:
Base() {
RegisterDrawCallback(std::bind(&Base::DrawCallback, this));
}
void Draw() {
for (auto it(drawCallbacks_.begin()); it != drawCallbacks_.end(); ++it)
(*it)();
}
protected:
typedef std::function<void(void)> DrawCallbackType;
typedef std::vector<DrawCallbackType> DrawSequence;
void RegisterDrawCallback(DrawCallbackType f) {
drawCallbacks_.push_back(f);
}
private:
void DrawCallback() { std::cout << "Base" << std::endl; }
DrawSequence drawCallbacks_;
};
class Derived : public Base {
public:
Derived() {
RegisterDrawCallback(std::bind(&Derived::DrawCallback, this));
}
private:
void DrawCallback() { std::cout << "Derived" << std::endl; }
};
class DerivedDerived : public Derived {
public:
DerivedDerived() {
RegisterDrawCallback(std::bind(&DerivedDerived::DrawCallback, this));
}
private:
void DrawCallback() { std::cout << "DerivedDerived" << std::endl; }
};
[这只是一个选项;别人可能想出一个更优雅的解决方案。就个人而言,我只是确保虚拟成员函数有详细的文档,并保留它。]
[This is just one option; someone else can probably come up with a far more elegant solution. Personally, I'd just make sure the virtual member functions are well-documented and leave it at that.]
这篇关于如何强制孩子使用相同的虚函数首先调用其父虚函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!