c ++标准做法:虚拟接口类与模板 [英] c++ standard practice: virtual interface classes vs. templates

查看:113
本文介绍了c ++标准做法:虚拟接口类与模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须对泛化和多态性做出决定。

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不使用太多(或没有?)继承,或者。
但是我认为这些都是目标是真正小的基本工具,每个
程序员使用两行代码。

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.

我的场景有点在中间。

我不需要在运行时交换代码片段,编译时间很好。
通常它也是一个非常核心和频繁使用的功能,
它不是逻辑上可分为大块。

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的副本有效的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)他们通常必须在头文件中定义(这意味着更多的代码被#included),这通常会导致更慢的编译时间。

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 ++标准做法:虚拟接口类与模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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