Is there a difference between the use of & ldquo; This & rdquo; Pointer...
source link: https://www.codesd.com/item/is-there-a-difference-between-the-use-of-this-pointer-and-not-use-it.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Is there a difference between the use of & ldquo; This & rdquo; Pointer and not use it?
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:
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.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK