c++ - 为什么不抽象政策?

标签 c++ boost policy

在实现策略时,需要遵循特定的接口(interface)。据我了解,这些政策必须能够相互替代。在现代 c++ 书中,Ch 1.5 3 策略具有相同的接口(interface)“T* Create() {}”。为什么不需要抽象它。如果策略应该有多个接口(interface),那将很重要。据我所知,抽象类给出了接口(interface)应该在具体类(策略类)中的方法。在Wikipedia example “使用”定义策略应该具有哪些接口(interface),但它不是通过抽象类。抽象类的目的不就是确保派生类具有所需的接口(interface)吗?

我错过了什么?

最佳答案

不同之处在于,使用抽象基类的接口(interface)具有提供运行时多态性的虚函数。

策略用于为模板提供编译时多态性。编译器会注意到您的策略类是否具有 T* Create()。否则,您将在尝试使用它时遇到编译时错误。

关于c++ - 为什么不抽象政策?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9643333/

相关文章:

c++ - 如何 fold STL 容器?

c++ - 这两个查找回文的函数之间的区别

c++ - boost 日志 : How to prevent the output will be duplicated to all added streams when it uses the add_file_log() function?

c++ - boost::trim_right_if 和空字符

amazon-s3 - 是否有 S3 策略限制访问只能查看/访问一个存储桶?

json - Azure DataFactory 无法使用定义检查版本

c++ - SetProgressValue() 在 ConEmu 中不起作用

c++ - 从 ‘const int*’ 到 ‘int*’ 的无效转换

c++ - 重现/调试一些多线程 hell

java - 在运行时更新 Java 安全策略?