是否有开销使用this->在c ++? [英] Is there overhead using this-> in c++?
问题描述
我有一个类,如果我想从一个成员函数中引用一个成员变量,我可以写:
I have a class, and if I wish to reference a member variable from within a member function, i could write:
this-> myVar = 10 //例如
或者我可以写:
myVar = 10
我喜欢使用this->,因为它明确声明变量是此类的成员,只是使用变量名称本身?
I like to use this-> because it explicitly declares that the variable is a member of this class, but does it occur any overhead in comparison to just using the variable name by itself?
作为替代,我可以在vars添加一个唯一的前缀,如 _TmyVar
As an alternative I could maybe add a unique prefix to the vars, such as _TmyVar, but I've been using this-> for a long time so I just wondered.
提前感谢Tom。
推荐答案
没有开销。编译器将为这两个版本生成完全相同的代码。
There is no overhead. The compiler will generate the exact same code for both versions.
这篇关于是否有开销使用this->在c ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!