柚子快報(bào)激活碼778899分享:C++:this指針詳解
柚子快報(bào)激活碼778899分享:C++:this指針詳解
目錄
一、this指針
?二、C++和C語(yǔ)言實(shí)現(xiàn)Stack對(duì)比
一、this指針
? Date類中有 Init 與 Print 兩個(gè)成員函數(shù),函數(shù)體中沒有關(guān)于不同對(duì)象的區(qū)分,那當(dāng)d1調(diào)用Init和 Print函數(shù)時(shí),該函數(shù)是如何知道應(yīng)該訪問的是d1對(duì)象還是d2對(duì)象呢?那么這里就要看到C++給了一個(gè)隱含的this指針解決這里的問題
? 編譯器編譯后,類的成員函數(shù)默認(rèn)都會(huì)在形參第?個(gè)位置,增加?個(gè)當(dāng)前類類型的指針,叫做this 指針,比如Date類的Init的真實(shí)原型為, void Init(Date* const this, int year, int month, int day)
? 類的成員函數(shù)中訪問成員變量,本質(zhì)都是通過this指針訪問的,如Init函數(shù)中給_year賦值, this- >_year = year;
? C++規(guī)定不能在實(shí)參和形參的位置顯示的寫this指針(編譯時(shí)編譯器會(huì)處理),但是可以在函數(shù)體內(nèi)顯示使用this指針。
class Date
{
public:
void Init(int year, int month, int day)
{
_year = year;
_month = month;
_day = day;
}
void Print()
{
cout << _year << "/" << _month << "/" << _day << endl;
}
private:
// 這?只是聲明,沒有開空間
int _year;
int _month;
int _day;
};
運(yùn)行下面兩串代碼,會(huì)發(fā)現(xiàn)兩個(gè)代碼雖然只差了一行代碼,結(jié)果卻差別很大
#include
using namespace std;
class A
{
public:
void Print()
{
cout << "A::Print()" << endl;
}
private:
int _a;
};
int main()
{
A* p = nullptr;
p->Print();
return 0;
}
#include
using namespace std;
class A
{
public:
void Print()
{
cout << "A::Print()" << endl;
cout << _a << endl;
}
private:
int _a;
};
int main()
{
A* p = nullptr;
p->Print();
return 0;
}
? ? ?這里先說(shuō)結(jié)果,第一串代碼能正常運(yùn)行,第二串代碼會(huì)運(yùn)行崩潰,第一串代碼在調(diào)用了p->Print時(shí)這個(gè)函數(shù),但是并沒有對(duì)p這個(gè)空指針進(jìn)行解引用,所以第一串代碼能夠正常運(yùn)行,而第二串代碼,再調(diào)用print函數(shù)之后,里面的_a實(shí)際是this->_a,對(duì)p進(jìn)行了解引用,而p是空指針,而對(duì)于空指針的訪問只有在運(yùn)行時(shí)才會(huì)發(fā)生,編譯器在編譯階段不會(huì)發(fā)現(xiàn),所以不會(huì)程序報(bào)錯(cuò),會(huì)在運(yùn)行時(shí)崩潰。
? ? ?在C++中,this指針是一個(gè)隱含的指向當(dāng)前對(duì)象的指針,它不是由程序員分配的,而是編譯器自動(dòng)創(chuàng)建并插入到每個(gè)成員函數(shù)的隱式參數(shù)列表中的,this指針位于函數(shù)調(diào)用幀的局部變量區(qū),通常稱為??臻g,用于存儲(chǔ)函數(shù)執(zhí)行時(shí)需要的臨時(shí)數(shù)據(jù)和指向自身對(duì)象的數(shù)據(jù),當(dāng)函數(shù)被調(diào)用時(shí),this?指針會(huì)被初始化為指向調(diào)用它的對(duì)象實(shí)例,這對(duì)于訪問類的私有成員變量特別有,this?永遠(yuǎn)不會(huì)為空,除非是在靜態(tài)成員函數(shù)或者非成員函數(shù)中,這時(shí)沒有特定的對(duì)象關(guān)聯(lián)。
?二、C++和C語(yǔ)言實(shí)現(xiàn)Stack對(duì)比
? ? 面向?qū)ο笕筇匦裕悍庋b、繼承、多態(tài),下面的對(duì)比我們可以初步了解?下封裝,通過下面兩份代碼對(duì)比,我們發(fā)現(xiàn)C++實(shí)現(xiàn)Stack形態(tài)上還是發(fā)生了挺多的變化,底層和邏輯上沒啥變 化。
C實(shí)現(xiàn)stack代碼
#include
#include
#include
#include
typedef int STDataType;
typedef struct Stack
{
STDataType* a;
int top;
int capacity;
}ST;
void STInit(ST* ps)
{
assert(ps);
ps->a = NULL;
ps->top = 0;
ps->capacity = 0;
}
void STDestroy(ST* ps)
{
assert(ps);
free(ps->a);
ps->a = NULL;
ps->top = ps->capacity = 0;
}
void STPush(ST* ps, STDataType x)
{
assert(ps);
if (ps->top == ps->capacity)
{
int newcapacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
STDataType* tmp = (STDataType*)realloc(ps->a, newcapacity *
sizeof(STDataType));
if (tmp == NULL)
{
perror("realloc fail");
return;
}
ps->a = tmp;
ps->capacity = newcapacity;
}
ps->a[ps->top] = x;
ps->top++;
}
bool STEmpty(ST* ps)
{
assert(ps);
return ps->top == 0;
}
void STPop(ST* ps)
{
assert(ps);
assert(!STEmpty(ps));
ps->top--;
}
STDataType STTop(ST* ps)
{
assert(ps);
assert(!STEmpty(ps));
return ps->a[ps->top - 1];
}
int STSize(ST* ps)
{
assert(ps);
return ps->top;
}
int main()
{
ST s;
STInit(&s);
STPush(&s, 1);
STPush(&s, 2);
STPush(&s, 3);
STPush(&s, 4);
while (!STEmpty(&s))
{
printf("%d\n", STTop(&s));
STPop(&s);
}
STDestroy(&s);
return 0;
}
C++實(shí)現(xiàn)stack代碼
#include
using namespace std;
typedef int STDataType;
class Stack
{
public:
// 成員函數(shù)
void Init(int n = 4)
{
_a = (STDataType*)malloc(sizeof(STDataType) * n);
if (nullptr == _a)
{
perror("malloc申請(qǐng)空間失敗");
return;
}
_capacity = n;
_top = 0;
}
void Push(STDataType x)
{
if (_top == _capacity)
{
int newcapacity = _capacity * 2;
STDataType* tmp = (STDataType*)realloc(_a, newcapacity *
sizeof(STDataType));
if (tmp == NULL)
{
perror("realloc fail");
return;
}
_a = tmp;
_capacity = newcapacity;
}
_a[_top++] = x;
}
void Pop()
{
assert(_top > 0);
--_top;
}
bool Empty()
{
return _top == 0;
}
int Top()
{
assert(_top > 0);
return _a[_top - 1];
}
void Destroy()
{
free(_a);
_a = nullptr;
_top = _capacity = 0;
}
private:
// 成員變量
STDataType* _a;
size_t _capacity;
size_t _top;
};
int main()
{
Stack s;
s.Init();
s.Push(1);
s.Push(2);
s.Push(3);
s.Push(4);
while (!s.Empty())
{
printf("%d\n", s.Top());
s.Pop();
}
s.Destroy();
return 0;
}
? C++中數(shù)據(jù)和函數(shù)都放到了類里面,通過訪問限定符進(jìn)行了限制,不能再隨意通過對(duì)象直接修改數(shù)據(jù),這是C++封裝的一種體現(xiàn),這個(gè)是最重要的變化,這里的封裝的本質(zhì)是一種更嚴(yán)格規(guī)范的管 理,避免出現(xiàn)亂訪問修改的問題,當(dāng)然封裝不僅僅是這樣的,我們后面還需要不斷的去學(xué)習(xí)。
? C++中有一些相對(duì)方便的語(yǔ)法,比如 Init 給的缺省參數(shù)會(huì)方便很多,成員函數(shù)每次不需要傳對(duì)象地 址,因?yàn)閠his指針隱含的傳遞了,方便了很多,使用類型不再需要typedef用類名就很方便。
? ? ? 本篇關(guān)于this指針的內(nèi)容就到這里了,希望對(duì)各位有幫助,如果有錯(cuò)誤歡迎各位指出。
柚子快報(bào)激活碼778899分享:C++:this指針詳解
精彩內(nèi)容
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點(diǎn)和立場(chǎng)。
轉(zhuǎn)載請(qǐng)注明,如有侵權(quán),聯(lián)系刪除。