Does using “this” pointer adds another operation to the program at runtime?
Just to give an example to explain the question better:
class C
{
public:
void set_x(int val){ x = val; }
void set_this_x(int val){ this->x = val; }
private:
int x;
};
Does the function “C::set_x()”, during runtime, performs 1 less operation than “C::set_this_x()” ?
Thanks! 🙂
There is no difference between the two member functions. It has to be, since this is what the C++ Standard (ISO/IEC 14882:2003) has to say:
So that means the following code:
would’ve been transformed to the following code according to 9.3.1/2 and 5.2.5/3:
To show that there really is no difference, at least for one compiler, here’s a side-by-side comparison of the disassembly of the
C::set_x()andC::set_this_x()function the VC++ compiler emits with optimizations disabled (/Od):Note that the compiler produces the exact same assembly for both member functions.