简体   繁体   English

我可以同时实现动态多态和静态多态的所有功能吗?

[英]Can I achieve all the functionality of dynamic polymorphism with static polymorphism?

I am used to use dynamic polymorphism and everything works fine but after reading about static polymorphism I concluded that this latter overcomes the overheads of dynamic one. 我习惯于使用dynamic polymorphism并且一切正常,但是在阅读了static polymorphism我得出的结论是,后者消除了动态static polymorphism的开销。

Here are some overhead of dynamic polymorphism: 这是动态多态性的一些开销:

  • Extra indirection (pointer dereference) for each call to a virtual method. 每次对虚拟方法的调用都需要额外的间接调用(指针取消引用)。

  • Virtual methods usually can't be inlined, which may be a significant cost hit for some small methods. 通常无法内联虚拟方法,对于某些小型方法而言,这可能会造成巨大的成本损失。

  • Additional pointer per object. 每个对象的附加指针。 On 64-bit systems which are prevalent these days, this is 8 bytes per object. 在当今流行的64位系统上,每个对象8个字节。 For small objects that carry little data this may be a serious overhead. 对于携带少量数据的小对象,这可能是一个严重的开销。

Can anyone explains to me Static polymorphism with a very simple example. 谁能用一个非常简单的例子向我解释Static polymorphism And when cases I should use it instead of the dynamic one? 在什么情况下我应该使用它而不是动态的?

I found this example but sorry I don't understand it. 我找到了这个示例,但是对不起,我不明白。 It looks ambiguous: 看起来模棱两可:

#include <iostream>
using namespace std;

template <typename Child>
struct Base
{
    void interface()
    {
        static_cast<Child*>(this)->implementation();
    }
};

struct Derived : Base<Derived>
{
    void implementation()
    {
        cerr << "Derived implementation\n";
    }
};

int main()
{
    Derived d;
    d.interface();  // Prints "Derived implementation"
}

Let's first analyze what will happen after compiling code you sent. 让我们首先分析一下在编译发送的代码之后会发生什么。 Compiler will generate two classes, and inside of method interface, it will just move pointer to address where object of Derived located and will call method implementation with that pointer. 编译器将生成两个类,并且在方法接口内部,它将仅将指针移动到Derived对象所在的地址,并使用该指针调用方法实现。

So now only overhead we have, is that we should generate more classes for each derived class (I mean binary code). 因此,现在只有开销,那就是我们应该为每个派生类生成更多的类(我的意思是二进制代码)。

What you are missing now? 你现在想念什么?

Consider following code: 考虑以下代码:

Base* base;

if(a)
  base = new DerivedA;
else
  base = new DerivedB;

base->interface();

The value of variable a is only known at runtime, so you can only use dynamic polymorphism for it. 变量a的值仅在运行时才知道,因此只能对其使用动态多态。

If it was static polymorphism, you should have told what will be type of derived class. 如果它是静态多态性,那么您应该已经知道派生类的类型。

Base<DerivedA> * base;
Base<DerivedB> * base;

Which not allows you to decide for it, dependent on runtime context. 哪一个不允许您根据运行时上下文来决定。

Static polymorphism is an alternative name for function overloading. 静态多态性是函数重载的替代名称。 This is a convenience feature, which lets you avoid renaming a function simply to take an alternative set of parameters. 这是一项便利功能,可让您避免仅使用另一组参数即可重命名功能。

It is completely orthogonal to dynamic polymorphism, which is an actual dispatch mechanism, when the function is decided at run-time. 当函数在运行时确定时,它与实际的调度机制动态多态性完全正交。

Note: the overhead of dynamic polymorphism is so small that you should not consider it until you profile the program and see that the overhead is significant. 注意:动态多态性的开销是如此之小,以至于在对程序进行概要分析并发现开销很大之前,您不应该考虑它。 This happens very infrequently. 这种情况很少发生。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM