这种间接函数调用的优点是什么?
我在库中找到了以下代码:
class Bar {
public:
bool foo(int i) {
return foo_(i);
}
private:
virtual bool foo_(int i) = 0;
};
现在我想知道:为什么要使用这种间接寻址? 是否有任何理由说明上述方法比简单的替代方案更好:
class Bar {
public:
virtual bool foo(int i) = 0;
};
I found the following code in a library:
class Bar {
public:
bool foo(int i) {
return foo_(i);
}
private:
virtual bool foo_(int i) = 0;
};
Now I'm wondering: Why would you use this indirection? Could there be any reasons why the above would be better than the simple alternative:
class Bar {
public:
virtual bool foo(int i) = 0;
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这是非虚拟接口惯用法 (NVI)。 Herb Sutter 的那一页有很多关于它的细节。 然而,请用 C++ FAQ Lite 所说的内容来调整你在那里读到的内容 此处和此处。
NVI 的主要优点是将接口与实现分离。 基类可以实现通用算法并将其呈现给世界,而其子类可以通过虚函数实现算法的细节。 外部用户不会受到算法细节变化的影响,特别是当您后来决定添加预处理和后处理代码时。
明显的缺点是您必须编写额外的代码。 此外,
私有
虚函数让很多人感到困惑。 许多编码员错误地认为你不能覆盖它们。 Herb Sutter 似乎喜欢私有
虚拟,但恕我直言,在实践中遵循 C++ FAQ Lite 的建议并使它们受到保护
会更有效。This is the Non-Virtual Interface Idiom (NVI). That page by Herb Sutter has a good bit of detail about it. However, temper what you read there with what the C++ FAQ Lite says here and here.
The primary advantage of NVI is separating interface from implementation. A base class can implement a generic algorithm and present it to the world while its subclasses can implement the details of the algorithm through virtual functions. Outside users are shielded from changes in the algorithm details, especially if you later decide you want to do add pre- and post-processing code.
The obvious disadvantage is that you have to write extra code. Also,
private
virtual functions are confusing to a lot of people. Many coders mistakenly think you can't override them. Herb Sutter seems to likeprivate
virtuals, but IMHO it's more effective in practice to follow the C++ FAQ Lite's recommendation and make themprotected
.这是模板模式。 foo 方法包含所有子类必须执行的代码。 当您这样看时,它更有意义:
它比替代方案更好,即尝试记住单独调用每个子类中的公共代码。 不可避免地,有人创建了一个子类,但忘记调用公共代码,从而导致许多问题。
This is the template pattern. The foo method contains code that must be executed by all subclasses. It makes more sense when you look at it like this:
It's nicer than the alternative, which is to try to remember to call the common code in each subclass individually. Inevitably, someone makes a subclass but forgets to call the common code, resulting in any number of problems.
这通常称为 Template-Hook 对(又名 Hotspot),由 Wolfgang Pree 创造。
请参阅此PDF,PowerPoint,HTML
进行您所说的间接的一个原因是,事情通常可以/必须在方法之前设置 ,以及一些清理后方法调用。 在子类中,您只需要提供必要的行为,而无需进行设置和清理...
This is often called a Template-Hook pair (a.k.a Hotspot), coined by Wolfgang Pree.
See this PDF, PowerPoint, HTML
One reason for doing the indirection as you call it is that things often can/has to be setup prior a method, and some cleaing post a method call. In the subclasses you only need to supply the necessary behaviour without doing the setup and cleaning...
如果子类可以更改 foo_? 的定义,但消费者需要一个静态函数(为了提高效率)? 或者委托模式?
If a subclass could change the definition of foo_?, but the consumers needed a static function (for efficiency)? Or for a delegation pattern?