Is accessing c++ member class through “this->membe

2019-04-05 14:34发布

After some searching on our friend google, I could not get a clear view on the following point.

I'm used to call class members with this->. Even if not needed, I find it more explicit as it helps when maintaining some heavy piece of algorithm with loads of vars.

As I'm working on a supposed-to-be-optimised algorithm, I was wondering whether using this-> would alter runtime performance or not.

Does it ?

4条回答
甜甜的少女心
2楼-- · 2019-04-05 14:59

No, the call is exactly the same in both cases.

查看更多
时光不老,我们不散
3楼-- · 2019-04-05 15:04

Answer has been given by zennehoy and here's assembly code (generated by Microsoft C++ compiler) for a simple test class:

class C
{
    int n;
public:
    void boo(){n = 1;}
    void goo(){this->n = 2;}
};

int main()
{
   C c;
   c.boo();
   c.goo();

       return 0;
}

Disassembly Window in Visual Studio shows that assembly code is the same for both functions:

class C
{
    int n;
public:
    void boo(){n = 1;}
001B2F80 55                   push        ebp  
001B2F81 8B EC                mov         ebp,esp  
001B2F83 81 EC CC 00 00 00    sub         esp,0CCh  
001B2F89 53                   push        ebx  
001B2F8A 56                   push        esi  
001B2F8B 57                   push        edi  
001B2F8C 51                   push        ecx  
001B2F8D 8D BD 34 FF FF FF    lea         edi,[ebp-0CCh]  
001B2F93 B9 33 00 00 00       mov         ecx,33h  
001B2F98 B8 CC CC CC CC       mov         eax,0CCCCCCCCh  
001B2F9D F3 AB                rep stos    dword ptr es:[edi]  
001B2F9F 59                   pop         ecx  
001B2FA0 89 4D F8             mov         dword ptr [ebp-8],ecx  
001B2FA3 8B 45 F8             mov         eax,dword ptr [this]  
001B2FA6 C7 00 01 00 00 00    mov         dword ptr [eax],1  
001B2FAC 5F                   pop         edi  
001B2FAD 5E                   pop         esi  
001B2FAE 5B                   pop         ebx  
001B2FAF 8B E5                mov         esp,ebp  
001B2FB1 5D                   pop         ebp  
001B2FB2 C3                   ret  
...
--- ..\main.cpp -----------------------------
void goo(){this->n = 2;}
001B2FC0 55                   push        ebp  
001B2FC1 8B EC                mov         ebp,esp  
001B2FC3 81 EC CC 00 00 00    sub         esp,0CCh  
001B2FC9 53                   push        ebx  
001B2FCA 56                   push        esi  
001B2FCB 57                   push        edi  
001B2FCC 51                   push        ecx  
001B2FCD 8D BD 34 FF FF FF    lea         edi,[ebp-0CCh]  
001B2FD3 B9 33 00 00 00       mov         ecx,33h  
001B2FD8 B8 CC CC CC CC       mov         eax,0CCCCCCCCh  
001B2FDD F3 AB                rep stos    dword ptr es:[edi]  
001B2FDF 59                   pop         ecx  
001B2FE0 89 4D F8             mov         dword ptr [ebp-8],ecx  
001B2FE3 8B 45 F8             mov         eax,dword ptr [this]  
001B2FE6 C7 00 02 00 00 00    mov         dword ptr [eax],2  
001B2FEC 5F                   pop         edi  
001B2FED 5E                   pop         esi  
001B2FEE 5B                   pop         ebx  
001B2FEF 8B E5                mov         esp,ebp  
001B2FF1 5D                   pop         ebp  
001B2FF2 C3                   ret  

And the code in the main:

    C c;
    c.boo();
    001B2F0E 8D 4D F8             lea         ecx,[c]  
    001B2F11 E8 00 E4 FF FF       call        C::boo (1B1316h)  
    c.goo();
    001B2F16 8D 4D F8             lea         ecx,[c]  
    001B2F19 E8 29 E5 FF FF       call        C::goo (1B1447h)  

Microsoft compiler uses __thiscall calling convention by default for class member calls and this pointer is passed via ECX register.

查看更多
家丑人穷心不美
4楼-- · 2019-04-05 15:07

There are several layers involved in the compilation of a language.

The difference between accessing member as member, this->member, MyClass::member etc... is a syntactic difference.

More precisely, it's a matter of name lookup, and how the front-end of the compiler will "find" the exact element you are referring to. Therefore, you might speed up compilation by being more precise... though it will be unnoticeable (there are much more time-consuming tasks involved in C++, like opening all those includes).

Since (in this case) you are referring to the same element, it should not matter.


Now, an interesting parallel can be done with interpreted languages. In an interpreted language, the name lookup will be delayed to the moment where the line (or function) is called. Therefore, it could have an impact at runtime (though once again, probably not really noticeable).

查看更多
家丑人穷心不美
5楼-- · 2019-04-05 15:20

It doesn't make any difference. Here's a demonstration with GCC. The source is simple class, but I've restricted this post to the difference for clarity.

% diff -s with-this.cpp without-this.cpp
7c7
<         this->x = 5;
---
>         x = 5;

% g++ -c with-this.cpp without-this.cpp  

% diff -s with-this.o without-this.o   
Files with-this.o and without-this.o are identical
查看更多
登录 后发表回答