我应该停止使用抽象基类/接口,而改用boost :: function / std :: function? [英] Should I stop using abstract base classes/interfaces and instead use boost::function/std::function?

查看:137
本文介绍了我应该停止使用抽象基类/接口,而改用boost :: function / std :: function?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚了解了std :: function的真正意义以及它的用途,我有一个问题:现在我们基本上有委托,我们应该在何时何地使用抽象基类,我们应该通过std :: function对象实现多态性喂给一个通用类? ABC在C ++ 11中受到致命打击吗?

I've just learned about what std::function really is about and what it is used for and I have a question: now that we essentially have delegates, where and when should we use Abstract Base Classes and when, instead, we should implement polymorphism via std::function objects fed to a generic class? Did ABC receive a fatal blow in C++11?

我个人的经验是,切换代理比为每个特定行为创建多个继承类更容易编码...所以我有点困惑abotu如何有用的抽象基础将从现在开始。

Personally my experience so far is that switching delegates is much simpler to code than creating multiple inherited classes each for particular behaviour... so I am a little confused abotu how useful Abstract Bases will be from now on.

推荐答案

回调

std :: function 的问题(以前 boost :: function )是大多数时候你需要回调一个类方法,因此需要绑定这个到函数对象。但是在调用代码中,你没有办法知道这个是否还在。事实上,你不知道有一个 this ,因为bind已经将调用函数的签名塑造成了调用者需要的。

The problem with std::function (previously boost::function) is that most of the time you need to have a callback to a class method, and therefore need to bind this to the function object. However in the calling code, you have no way to know if this is still around. In fact, you have no idea that there even is a this because bind has molded the signature of the calling function into what the caller requires.

这可能会导致奇怪的崩溃,因为回调试图触发不再存在的类的方法。

This can naturally cause weird crashes as the callback attempts to fire into methods for classes that no longer exist.

你可以使用< a href =http://www.boost.org/doc/libs/1_40_0/libs/smart_ptr/enable_shared_from_this.html> shared_from_this 并绑定shared_ptr到回调,但是你的实例可能永远不会消失。有回调给你的人现在参与你的所有权,而他们甚至不知道。你可能想要更可预测的所有权和破坏。

You can, of course use shared_from_this and bind a shared_ptr to a callback, but then your instance may never go away. The person that has a callback to you now participates in your ownership without them even knowing about it. You probably want more predictable ownership and destruction.

另一个问题,即使你可以让回调工作正常,是回调,代码也可以解耦。对象之间的关系可能难以确定代码可读性降低。然而,接口在适当级别的去耦与在接口的契约中定义的明确指定的关系之间提供良好的折衷。

Another problem, even if you can get the callback to work fine, is with callbacks, the code can be too decoupled. The relationships between objects can be so difficult to ascertain that the code readability becomes decreased. Interfaces, however, provide a good compromise between an appropriate level of decoupling with a clearly specified relationship as defined be the interface's contract. You can also more clearly specify, in this relationship, issues like who owns whom, destrcution order, etc.

std ::的额外问题函数是许多调试器不支持他们。在VS2008和boost函数中,你必须通过大约7层才能得到你的函数。即使所有其他事情是相同的回调是最好的选择,纯粹的烦恼和时间浪费意外跨过 std :: function 的目标是足够的理由,以避免它。继承是语言的核心特征,并且步进到接口的重写方法是即时的。

An additional problem with std::function is that many debuggers do not support them well. In VS2008 and boost functions, you have to step through about 7 layers to get to your function. Even if all other things being equal a callback was the best choice, the sheer annoyance and time wasted accidentally stepping over the target of std::function is reason enough to avoid it. Inheritance is a core feature of the language, and stepping into an overridden method of an interface is instantaneous.

最后,我要添加我们没有C ++中的委托。 C#中的代表是语言的核心部分,就像继承在C ++和C#中一样。我们有一个std库功能,IMO是从核心语言功能中删除一个层。所以它不会与语言的其他核心功能紧密集成。它反而有助于形式化的函数对象的概念已经是一个C ++成语为好的现在。

Lastly I'll just add we don't have delegates in C++. Delegates in C# are a core part of the language, just like inheritance is in C++ and C#. We have a std library feature which IMO is one layer removed from a core language functionality. So its not going to be as tightly integrated with other core features of the language. It instead helps formalize the idea of function objects that have been a C++ idiom for a good while now.

这篇关于我应该停止使用抽象基类/接口,而改用boost :: function / std :: function?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆