我必须就泛化与多态性做出决定.
I have to make a decision regarding generalization vs polymorphism.
这个场景是标准的:我想让我的整体相互依赖代码更加模块化、干净和可扩展.还处于可以改变设计原则的阶段,而且,在我看来,非常可取.
Well the scenario is standard: I want to make my monolithic interdependent code to be more modular, clean and extensible. It is still in a stage where the change of design principle is doable, and, as I look at it, highly desirable.
我会引入纯虚拟基类(接口)还是模板?
Will I introduce purely virtual base classes (interfaces) or templates?
我了解有关模板选项的基础知识:更少的间接性,更好的性能,更多的编译但没有后期绑定等.
I am aware of the basics regarding the template option: less indirection, better performance, more compiling but no late binding, and so on.
stl 没有使用太多(或没有?)继承,boost 也没有.但我认为这些旨在成为每个人都使用的非常小的基本工具程序员的2行代码.
The stl does not use much (or none?) inheritance, and boost doesn't either. But I think those are aimed to be really small basic tools that are used every 2 lines of code by the programmer.
我认为继承和后期绑定方法更适合应该可以互换的大段代码和功能的插件风格,可在部署后甚至运行时更新等.
I consider the inheritance and late binding approach to be more sensible for plug-in style of big pieces of code and functionality that should be interchangeable, updateable etc. after deployment or even during runtime.
好吧,我的情况介于两者之间.
Well my scenario lies somewhat inbetween.
我不需要在运行时即时交换代码片段,编译时间很好.通常它也是一个非常核心和经常使用的功能,它在逻辑上不能分成大块.
I dont need to exchange pieces of code on the fly at runtime, compile time is fine. Usually it is also a very central and frequently used piece of functionality, it is not logically seperatable into big blocks.
这让我有点倾向于模板解决方案.对我来说,它看起来也更干净一些.
This lets me tend somewhat to the template solution. To me it also looks somewhat cleaner.
是否有任何严重的不良影响,接口仍然是方式去?他们什么时候不是?哪个更符合标准 C++ 风格?
Are there any big bad implications, are interfaces still THE way to go? When are they not? Which complies more with standard c++ style?
我知道这有点主观,但我真的很感兴趣一些经验.我没有 Scott Meyers Effective C++ 的副本所以我把希望寄托在你们身上:)
I know this is bordering on subjective, but I am really interested in some experiences. I don't own a copy of Scott Meyers effective C++ so I set my hopes on you guys :)
你基本上是对的,当类型应该允许在运行时改变时(例如在插件中),动态多态(继承、虚拟)通常是正确的选择架构).如果类型只应在编译时更改,则静态多态(模板)是更好的选择.
You're basically right, dynamic polymorphism (inheritance, virtuals) is generally the right choice when the type should be allowed to change at runtime (for example in plugin architectures). Static polymorphism (templates) is a better choice if the type should only change at compile-time.
模板唯一潜在的缺点是 1) 它们通常必须在头文件中定义(这意味着更多的代码被#include),这通常会导致编译时间变慢.
The only potential downsides to templates are that 1) they generally have to be defined in the headers (which means more code gets #included), and this often leads to slower compile-times.
但在设计方面,我看不出在可能的情况下使用模板有任何问题.
But design-wise, I can't see any problems in using templates when possible.
哪个更符合标准c++风格?
Which complies more with standard c++ style?
取决于什么是标准 C++ 风格".C++ 标准库使用了一些东西.STL 对一切都使用模板,稍旧的 IOStreams 库使用继承和虚函数,而从 C 继承的库函数当然没有使用.
Depends on what "standard C++ style" is. The C++ standard library uses a bit of everything. The STL uses templates for everything, the slightly older IOStreams library uses inheritance and virtual functions, and the library functions inherited from C uses neither, of course.
如今,模板是迄今为止最受欢迎的选择,我不得不说这是最标准"的方法.
These days, templates are by far the most popular choice though, and I'd have to say that is the most "standard" approach.
这篇关于C++ 标准实践:虚拟接口类与模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!