使用“this->”的性能惩罚 [英] Performance penalties on using "this->"?

查看:110
本文介绍了使用“this->”的性能惩罚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑类C中两个类似的C ++成员函数的示例:

Consider this example of two similar C++ member functions in a class C:

void C::function(Foo new_f) {
    f = new_f;
}

void C::function(Foo new_f) {
    this->f = new_f;
}

这些函数是否以相同的方式编译?使用 this-> (更多内存访问或任何内容)是否有任何性能损失?

Are these functions compiled in the same manner? Are there any performance penalties for using this-> (more memory accesses or whatever)?

推荐答案

是的,它是完全一样的,你会得到相同的性能。

Yes, it's exactly the same and you'll get the same performance.

使用 this-> 语法是当您具有与要访问的实例变量相同名称的函数的参数时。使用变量的名称本身将引用参数,因此您需要 this-> 。当然,你也可以重命名参数。正如ildjarn在评论中指出的,在某些情况下,你需要使用 this 来调用依赖的函数,因为 this 是隐式依赖的(你可以阅读更多关于它)。

The only time you really must use the this-> syntax is when you have an argument to the function with the same name as an instance variable you want to access. Using the name of the variable by itself will refer to the argument so you need the this->. Of course, you could just rename the argument too. And, as ildjarn has pointed out in the comments also, you need to use this in certain situations to call functions that are dependent because this is implicitly dependent (you can read more about that though).

这篇关于使用“this->”的性能惩罚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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