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! :-)
No, it doesn't make a runtime difference, it's just syntax. The this
pointer is still accessed in the first function, it's only specified implicitly instead of explicitly.
By the way, this smells like a premature optimization - write clean code first, fast code later.
The this->member
syntax is required if you inherit from a class template:
template<typename T>
class Base
{
protected:
T x;
};
template<typename T>
class Derived : Base<T>
{
public:
void whatever()
{
T a = x; // error
T b = this->x; // ok
}
};
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:
9.3.1 Nonstatic member functions [class.mfct.nonstatic]
2. When an
id-expression
(5.1) that is not part of a class member access syntax (5.2.5) and not used to form a pointer to member (5.3.1) is used in the body of a nonstatic member function of classX
or used in themem-initializer
for a constructor of classX
, if name lookup (3.4.1) resolves the name in theid-expression
to a nonstatic nontype member of classX
or of a base class ofX
, theid-expression
is transformed into a class member access expression (5.2.5) using(*this)
(9.3.2) as the postfix-expression to the left of the.
operator. The member name then refers to the member of the object for which the function is called.5.2.5 Class member access [expr.ref]
3. If
E1
has the type “pointer to classX
,” then the expressionE1->E2
is converted to the equivalent form(*(E1)).E2;
...
So that means the following code:
class C
{
public:
void set_x(int val) { x = val; }
void set_this_x(int val) { this->x = val; }
private:
int x;
};
would've been transformed to the following code according to 9.3.1/2 and 5.2.5/3:
class C
{
public:
void set_x(int val) { (*this).x = val; } // as per 9.3.1/2
void set_this_x(int val) { (*(this)).x = val; } // as per 5.2.5/3
private:
int x;
};
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()
and C::set_this_x()
function the VC++ compiler emits with optimizations disabled (/Od
):
void set_x(int val){ x = val; } void set_this_x(int val){ this->x = val; }
push ebp push ebp
mov ebp,esp mov ebp,esp
sub esp,0CCh sub esp,0CCh
push ebx push ebx
push esi push esi
push edi push edi
push ecx push ecx
lea edi,[ebp-0CCh] lea edi,[ebp-0CCh]
mov ecx,33h mov ecx,33h
mov eax,0CCCCCCCCh mov eax,0CCCCCCCCh
rep stos dword ptr es:[edi] rep stos dword ptr es:[edi]
pop ecx pop ecx
mov dword ptr [ebp-8],ecx mov dword ptr [ebp-8],ecx
mov eax,dword ptr [this] mov eax,dword ptr [this]
mov ecx,dword ptr [val] mov ecx,dword ptr [val]
mov dword ptr [eax],ecx mov dword ptr [eax],ecx
pop edi pop edi
pop esi pop esi
pop ebx pop ebx
mov esp,ebp mov esp,ebp
pop ebp pop ebp
ret 4 ret 4
Note that the compiler produces the exact same assembly for both member functions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With