c++ 和c 区别

c++是对c的拓展,c原有的语法c++都支持,并且在此基础上拓展语法:封装,继承,多态,模板,等等

值传递

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <Windows.h>
  4. struct Student
  5. {
  6. int a;
  7. int b;
  8. int c;
  9. int d;
  10. };
  11. int plus(Student s) {
  12. return s.a + s.b + s.c + s.d;
  13. }
  14. int main(int argc, char* argv[]) {
  15. Student s = { 1,2,3,4 };
  16. int r = plus(s);
  17. printf("%d", r);
  18. return 0;
  19. }
    Student s = { 1,2,3,4 };
003118F8  mov         dword ptr [s],1  
003118FF  mov         dword ptr [ebp-10h],2  
00311906  mov         dword ptr [ebp-0Ch],3  
0031190D  mov         dword ptr [ebp-8],4  
    int r = plus(s);
00311914  sub         esp,10h  
00311917  mov         eax,esp  
00311919  mov         ecx,dword ptr [s]  
0031191C  mov         dword ptr [eax],ecx  
0031191E  mov         edx,dword ptr [ebp-10h]  
00311921  mov         dword ptr [eax+4],edx  
00311924  mov         ecx,dword ptr [ebp-0Ch]  
00311927  mov         dword ptr [eax+8],ecx  
0031192A  mov         edx,dword ptr [ebp-8]  
0031192D  mov         dword ptr [eax+0Ch],edx  
00311930  call        plus (0311073h)  
00311935  add         esp,10h  
00311938  mov         dword ptr [r],eax

结构体作为参数传递,导致了大量内存复制操作,虽然结构体可以作为参数传递,但是不建议这么做。通常使用指针传递
**

指针传递

#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>

struct Student
{
    int a;
    int b;
    int c;
    int d;
};

int plus(Student* s) {
    return s->a+ s->b + s->c + s->d;
}

int main(int argc, char* argv[]) {
    Student s = { 1,2,3,4 };
    int r = plus(&s);
    printf("%d", r);
    return 0;
}
    Student s = { 1,2,3,4 };
00E71902  mov         dword ptr [s],1  
00E71909  mov         dword ptr [ebp-14h],2  
00E71910  mov         dword ptr [ebp-10h],3  
00E71917  mov         dword ptr [ebp-0Ch],4  
    int r = plus(&s);
00E7191E  lea         eax,[s]  
00E71921  push        eax  
00E71922  call        plus (0E713B6h)  
00E71927  add         esp,4  
00E7192A  mov         dword ptr [r],eax

int plus(Student* s) {
00E71770  push        ebp  
00E71771  mov         ebp,esp  
00E71773  sub         esp,0C0h  
00E71779  push        ebx  
00E7177A  push        esi  
00E7177B  push        edi  
00E7177C  lea         edi,[ebp-0C0h]  
00E71782  mov         ecx,30h  
00E71787  mov         eax,0CCCCCCCCh  
00E7178C  rep stos    dword ptr es:[edi]  
00E7178E  mov         ecx,offset _A192CE94_源@cpp (0E7C015h)  
00E71793  call        @__CheckForDebuggerJustMyCode@4 (0E7131Bh)  
    return s->a+ s->b + s->c + s->d;
00E71798  mov         eax,dword ptr [s]  
00E7179B  mov         eax,dword ptr [eax]  
00E7179D  mov         ecx,dword ptr [s]  
00E717A0  add         eax,dword ptr [ecx+4]  
00E717A3  mov         edx,dword ptr [s]  
00E717A6  add         eax,dword ptr [edx+8]  
00E717A9  mov         ecx,dword ptr [s]  
00E717AC  add         eax,dword ptr [ecx+0Ch]  
}

这样写起来很痛苦

c++ 帮助你操作

#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>

struct Student
{
    int a;
    int b;
    int c;
    int d;
    int plus() {
        return a+b+c+d;
    }
};

int main(int argc, char* argv[]) {
    Student s = { 1,2,3,4 };
    int r = s.plus();
    printf("%d", r);
    return 0;
}
    Student s = { 1,2,3,4 };
00591902  mov         dword ptr [s],1  
00591909  mov         dword ptr [ebp-14h],2  
00591910  mov         dword ptr [ebp-10h],3  
00591917  mov         dword ptr [ebp-0Ch],4  
    int r = s.plus();
0059191E  lea         ecx,[s]  
00591921  call        plus (05913BBh)  
00591926  mov         dword ptr [r],eax 


struct Student
{
    int a;
    int b;
    int c;
    int d;
    int plus() {
00591770  push        ebp  
00591771  mov         ebp,esp  
00591773  sub         esp,0CCh  
00591779  push        ebx  
0059177A  push        esi  
0059177B  push        edi  
0059177C  push        ecx  
0059177D  lea         edi,[ebp-0CCh]  
00591783  mov         ecx,33h  
00591788  mov         eax,0CCCCCCCCh  
0059178D  rep stos    dword ptr es:[edi]  
0059178F  pop         ecx  
00591790  mov         dword ptr [this],ecx  
00591793  mov         ecx,offset _A192CE94_源@cpp (059C015h)  
00591798  call        @__CheckForDebuggerJustMyCode@4 (059131Bh)  
        return a+b+c+d;
0059179D  mov         eax,dword ptr [this]  
005917A0  mov         eax,dword ptr [eax]  
005917A2  mov         ecx,dword ptr [this]  
005917A5  add         eax,dword ptr [ecx+4]  
005917A8  mov         edx,dword ptr [this]  
005917AB  add         eax,dword ptr [edx+8]  
005917AE  mov         ecx,dword ptr [this]  
005917B1  add         eax,dword ptr [ecx+0Ch]  
    }