Cpp1 封裝和this指針
封裝
C語(yǔ)言和C++語(yǔ)言的區(qū)別
C++是對(duì)C的補(bǔ)充擴(kuò)展,C原有的語(yǔ)法C++都支持,并在此基礎(chǔ)上擴(kuò)展了一些新的語(yǔ)法:
繼承、封裝、多態(tài)、模板等等
結(jié)構(gòu)體可以作為參數(shù)傳遞嗎
struct Student
{
int a;
int b;
int c;
int d;
}
//分析這個(gè)函數(shù)是如何傳遞參數(shù)的
int Plus(student s)
{
return s.a+s.b+s.c+s.d;
}
23: Student s = {1,2,3,4};
00401078 mov dword ptr [ebp-10h],1
0040107F mov dword ptr [ebp-0Ch],2
00401086 mov dword ptr [ebp-8],3
0040108D mov dword ptr [ebp-4],4
24: Plus(s);
00401094 sub esp,10h
00401097 mov eax,esp
00401099 mov ecx,dword ptr [ebp-10h]
0040109C mov dword ptr [eax],ecx
0040109E mov edx,dword ptr [ebp-0Ch]
004010A1 mov dword ptr [eax+4],edx
004010A4 mov ecx,dword ptr [ebp-8]
004010A7 mov dword ptr [eax+8],ecx
004010AA mov edx,dword ptr [ebp-4]
004010AD mov dword ptr [eax+0Ch],edx
004010B0 call @ILT+5(Plus) (0040100a)
004010B5 add esp,10h
16: int Plus(Student s)
17: {
00401020 push ebp
00401021 mov ebp,esp
00401023 sub esp,40h
00401026 push ebx
00401027 push esi
00401028 push edi
00401029 lea edi,[ebp-40h]
0040102C mov ecx,10h
00401031 mov eax,0CCCCCCCCh
00401036 rep stos dword ptr [edi]
18: return s.a+s.b+s.c+s.d;
00401038 mov eax,dword ptr [ebp+8]
0040103B add eax,dword ptr [ebp+0Ch]
0040103E add eax,dword ptr [ebp+10h]
00401041 add eax,dword ptr [ebp+14h]
19: }
//經(jīng)過(guò)觀察我們可以發(fā)現(xiàn),結(jié)構(gòu)體參數(shù)是通過(guò)復(fù)制結(jié)構(gòu)體傳遞參數(shù)的
將函數(shù)寫到結(jié)構(gòu)體里面,觀察反匯編
struct Student
{
int a;
int b;
int c;
int d;
int Plus()
{
return a+b+c+d;
}
};
int main()
{
Student s = {1,2,3,4};
s.Plus();
return 0;
}
14: int Plus()
15: {
00401060 push ebp
00401061 mov ebp,esp
00401063 sub esp,44h
00401066 push ebx
00401067 push esi
00401068 push edi
00401069 push ecx
0040106A lea edi,[ebp-44h]
0040106D mov ecx,11h
00401072 mov eax,0CCCCCCCCh
00401077 rep stos dword ptr [edi]
00401079 pop ecx
0040107A mov dword ptr [ebp-4],ecx
16: return a+b+c+d;
0040107D mov eax,dword ptr [ebp-4]
00401080 mov eax,dword ptr [eax]
00401082 mov ecx,dword ptr [ebp-4]
00401085 add eax,dword ptr [ecx+4]
00401088 mov edx,dword ptr [ebp-4]
0040108B add eax,dword ptr [edx+8]
0040108E mov ecx,dword ptr [ebp-4]
00401091 add eax,dword ptr [ecx+0Ch]
17: }
22: Student s = {1,2,3,4};
0040D4F8 mov dword ptr [ebp-10h],1
0040D4FF mov dword ptr [ebp-0Ch],2
0040D506 mov dword ptr [ebp-8],3
0040D50D mov dword ptr [ebp-4],4
23: s.Plus();
0040D514 lea ecx,[ebp-10h]
0040D517 call @ILT+10(Student::Plus) (0040100f)
24: return 0;
0040D51C xor eax,eax
//我們可以發(fā)現(xiàn)此時(shí)結(jié)構(gòu)體是通過(guò)指針傳參數(shù)。
//跟使用定義為指針的外部方法一樣的調(diào)用方式和處理方式
觀察結(jié)構(gòu)體的大小:
struct Student
{
int a;
int b;
int c;
int d;
int Plus()
{
return a+b+c+d;
}
};
int main()
{
Student s = {1,2,3,4};
s.Plus();
return 0;
}
//對(duì)比
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()
{
Student s = {1,2,3,4};
Plus(&s);
return 0;
}
//我們發(fā)現(xiàn)上述兩種定義方式基本一樣
結(jié)論:
在結(jié)構(gòu)體內(nèi)定義函數(shù),函數(shù)其實(shí)并不屬于這個(gè)結(jié)構(gòu)體,這樣做僅僅是為了使用方便。
將函數(shù)定義寫入到結(jié)構(gòu)體內(nèi)部,也就是我們所說(shuō)的封裝
封裝總結(jié)
- 什么是發(fā)封裝:將函數(shù)定義到結(jié)構(gòu)體內(nèi)部,就是封裝
- 什么是類:帶有函數(shù)的結(jié)構(gòu)體,稱為類
- 什么是成員函數(shù):結(jié)構(gòu)體里面的函數(shù),稱為成員函數(shù)
this指針
什么是this指針
struct Student
{
int a;
int b;
int c;
int d;
int Plus()
{
return a+b+c+d;
}
}
總結(jié):
- this指針是編譯器默認(rèn)傳入的,通常都會(huì)使用
ecx進(jìn)行參數(shù)的傳遞。 - 你用或者不用,他都在那。
this指針的使用
struct Student
{
int a;
int b;
void Init(int a,int b)
{
this->a=a;
this->b=b;
}
void Print()
{
printf("%d %d",a,b);
}
}
總結(jié):
- this指針不能做 ++ -- 等運(yùn)算,不能重新被賦值。
- this指針不占用結(jié)構(gòu)體的寬度 。
總結(jié)
- this指針是編譯器默認(rèn)傳入的,通常都會(huì)使用ecx進(jìn)行參數(shù)的傳遞。
- 成員函數(shù)都有this指針,無(wú)論是否使用。
- this指針不能做++ --等運(yùn)算,不能重新被賦值。
- this指針不占用結(jié)構(gòu)體的寬度。