使用虚拟继承的C ++ vtable解析 [英] C++ vtable resolving with virtual inheritance

查看:140
本文介绍了使用虚拟继承的C ++ vtable解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对C ++和虚拟继承感到好奇 - 特别是,在bass和子类之间解决vtable冲突的方式。我不会假装理解它们如何工作的细节,但是我所知道的是,它们是由于使用虚拟函数而导致的小延迟。我的问题是如果基类是空的 - 即,其虚函数定义为:

  virtual void doStuff 0; 

这是否意味着分辨率不是必需的,因为只有一组函数?



原谅我,如果这是一个愚蠢的问题 - 正如我所说,我不明白vtables如何工作,所以我真的不知道更好。

EDIT



所以如果我有一个抽象类有两个独立的子类:

  A 
/ \
/ \
BC
p>

从子类调用函数时没有性能损失,相比之下,只有一个继承自由类?

解决方案

在子类中调用非虚拟函数没有任何效果。如果你在你的例子中调用你的纯虚函数的覆盖版本,那么虚拟惩罚可能仍然存在。一般来说,编译器很难优化虚拟表的使用,除非在非常特殊的情况下,它知道有问题的对象(从上下文)的确切的按值类型。



但是认真不要担心开销。这将是如此之少,在实践中,你几乎肯定不会遇到的情况,它是导致性能瓶颈的代码的一部分。使用虚拟函数,使它们对您的设计有意义,而不必担心(微小的)性能损失。


I was curious about C++ and virtual inheritance - in particular, the way that vtable conflicts are resolved between bass and child classes. I won't pretend to understand the specifics on how they work, but what I've gleamed so far is that their is a small delay caused by using virtual functions due to that resolution. My question then is if the base class is blank - ie, its virtual functions are defined as:

virtual void doStuff() = 0;

Does this mean that the resolution is not necessary, because there's only one set of functions to pick from?

Forgive me if this is an stupid question - as I said, I don't understand how vtables work so I don't really know any better.

EDIT

So if I have an abstract class with two seperate child classes:

    A
   / \
  /   \
 B     C

There is no performance hit when calling functions from the child classes compared to say, just a single inheritance free class?

解决方案

There is no hit for calling nonvirtual functions in the child class. If you're calling an overridden version of your pure virtual function as in your example, then the virtual penalty may still exist. In general it's difficult for compilers to optimize away the use of the virtual table except under very specific circumstances, where it knows the exact by-value type of the object in question (from context).

But seriously don't worry about the overhead. It's going to be so little that in practice you will almost certainly never encounter a situation where it's the part of code causing performance bottlenecks. Use virtual functions where they make sense for your design and don't worry about the (tiny) performance penalty.

这篇关于使用虚拟继承的C ++ vtable解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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