静态绑定默认参数 [英] static binding of default parameter

查看:166
本文介绍了静态绑定默认参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Effective C ++中,这本书只提到了一个为什么默认参数是静态绑定的句子:

In Effective C++, the book just mentioned one sentence why default parameters are static bound:

如果默认参数值是动态绑定的,编译器将不得不提出确定虚拟函数在运行时的参数的适当默认值的方式,其比在编译期间确定它们的当前机制更慢和更复杂。

If default parameter values were dynamically bound, compilers would have to come up with a way to determine the appropriate default values for parameters of virtual functions at runtime, which would be slower and more complicated than the current mechanism of determining them during compilation.

任何人都可以多说几句?为什么它很复杂和低效?

Can anybody elaborate this a bit more? Why it is complicated and inefficient?

非常感谢!

推荐答案

每当一个类有虚函数时,编译器会生成一个所谓的v表来计算运行时需要的正确地址,以支持动态绑定和多态行为。许多类优化器努力去除虚函数为此原因。更少的开销,更小的代码。如果默认参数也被计算到方程中,它将使整个虚拟函数机制更麻烦和and肿。

Whenever a class has virtual functions, the compiler generates a so-called v-table to calculate the proper addresses that are needed at runtime to support dynamic binding and polymorphic behavior. Lots of class optimizers work toward removing virtual functions for this reason exactly. Less overhead, and smaller code. If default parameters were also calculated into the equation, it would make the whole virtual function mechanism all the more cumbersome and bloated.

这篇关于静态绑定默认参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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