Dyamic vs静态多态性在C ++:这是更好的? [英] Dyamic vs Static Polymorphism in C++ : which is preferable?

查看:234
本文介绍了Dyamic vs静态多态性在C ++:这是更好的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我理解动态/静态多态性取决于应用程序设计和要求。但是,如果可能的话,总是选择静态多态性是动态的建议吗?特别是,我可以在我的应用程序中看到以下2个设计选择,这两个设计选择似乎不建议这样做:


  1. 使用CRTP的静态多态性:没有vtable查找开销,同时仍然以模板基类的形式提供接口。


  2. 动态多态性:实现接口(纯虚拟类),关联(纯虚拟类),使用多个开关和static_cast访问正确的类/方法


我的应用程序非常重要,所以我赞成静态多态性。但是需要知道如果使用太多static_cast是一个糟糕的设计的指示,以及如何避免,而不会导致延迟。



编辑:感谢您的洞察。考虑一个具体情况,哪些是更好的方法?

  class IMessage_Type_1 
{
virtual long getQuantity()= 0;
...
}
class Message_Type_1_Impl:public IMessage_Type_1
{
long getQuantity(){return _qty;}
...
}

 code> template< class T> 
class TMessage_Type_1
{
long getQuantity(){return static_cast< T *>(this) - > getQuantity }
...
}
class Message_Type_1_Impl:public TMessage_Type_1< Message_Type_1_Impl>
{
long getQuantity(){return _qty; }
...
}

注意有几个mutators /在每个类中,我需要在我的应用程序中指定一个接口。在静态多态性中,我只切换一次 - 获取消息类型。然而,在动态多态性,我使用虚拟函数的方法调用。不,这使它使用静态聚合的情况下?我相信CRTP中的static_cast是相当安全的,没有性能损失(编译时间限制)?

解决方案

优化后的跳转序列变为跳转到由表查找的地址。完全像一个虚函数调用是。



如果你必须根据类型跳转,你必须先选择类型。如果选择不能在编译时完成(实质上是因为它取决于输入),则必须始终执行两个操作:select&跳。



事实上,你是重新创造 / em> v表。


I understand that dynamic/static polymorphism depends on the application design and requirements. However, is it advisable to ALWAYS choose static polymorphism over dynamic if possible? In particular, I can see the following 2 design choice in my application, both of which seem to be advised against:

  1. Implement Static polymorphism using CRTP: No vtable lookup overhead while still providing an interface in form of template base class. But, uses a Lot of switch and static_cast to access the correct class/method, which is hazardous

  2. Dynamic Polymorphism: Implement interfaces (pure virtual classes), associating lookup cost for even trivial functions like accessors/mutators

My application is very time critical, so am in favor of static polymorphism. But need to know if using too many static_cast is an indication of poor design, and how to avoid that without incurring latency.

EDIT: Thanks for the insight. Taking a specific case, which of these is a better approach?

class IMessage_Type_1
{
  virtual long getQuantity() =0;
...
}
class Message_Type_1_Impl: public IMessage_Type_1
{
  long getQuantity() { return _qty;}
...
}

OR

template <class T>
class TMessage_Type_1
{
  long getQuantity() { return static_cast<T*>(this)->getQuantity(); }
...
}
class Message_Type_1_Impl: public TMessage_Type_1<Message_Type_1_Impl>
{
  long getQuantity() { return _qty; }
...
}

Note that there are several mutators/accessors in each class, and I do need to specify an interface in my application. In static polymorphism, I switch just once - to get the message type. However, in dynamic polymorphism, I am using virtual functions for EACH method call. Doesnt that make it a case to use static poly? I believe static_cast in CRTP is quite safe and no performance penalty (compile time bound) ?

解决方案

A switch is nothing more than a sequence of jumps that -after optimized- becomes a jump to an address looked-up by a table. Exactly like a virtual function call is.

If you have to jump depending on a type, you must first select the type. If the selection cannot be done at compile time (essentially because it depends on the input) you must always perform two operation: select & jump. The syntactic tool you use to select doesn't change the performance, since optimize the same.

In fact you are reinventing the v-table.

这篇关于Dyamic vs静态多态性在C ++:这是更好的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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