C++中this指針用法示例詳解
一、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<iostream>
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<iostream>
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;
}
這里先說結(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語言實(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<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#include<assert.h>
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<iostream>
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ì)方便的語法,比如 Init 給的缺省參數(shù)會(huì)方便很多,成員函數(shù)每次不需要傳對(duì)象地 址,因?yàn)閠his指針隱含的傳遞了,方便了很多,使用類型不再需要typedef用類名就很方便。
總結(jié)
到此這篇關(guān)于C++中this指針用法示例詳解的文章就介紹到這了,更多相關(guān)C++ this指針詳解內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于Qt開發(fā)一個(gè)簡(jiǎn)單的OFD閱讀器
這篇文章主要為大家詳細(xì)介紹了如何使用Qt框架開發(fā)一個(gè)功能強(qiáng)大且性能優(yōu)異的OFD閱讀器,文中的示例代碼講解詳細(xì),有需要的小伙伴可以參考一下2025-01-01
Qt實(shí)現(xiàn)指針式時(shí)鐘 Qt實(shí)現(xiàn)動(dòng)態(tài)時(shí)鐘
這篇文章主要為大家詳細(xì)介紹了Qt實(shí)現(xiàn)指針式時(shí)鐘,Qt實(shí)現(xiàn)動(dòng)態(tài)時(shí)鐘,兩者相互切換,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07
C++玫瑰花字符畫項(xiàng)目實(shí)戰(zhàn)教程
本文介紹了C++字符藝術(shù)——以“玫瑰花代碼”為例,從字符選擇、循環(huán)與條件控制、對(duì)稱性實(shí)現(xiàn)、函數(shù)組織和用戶交互等方面,講解如何用C++編寫和優(yōu)化字符圖案,并支持個(gè)性化定制和動(dòng)態(tài)圖案生成2025-10-10
基于C中一個(gè)行壓縮圖的簡(jiǎn)單實(shí)現(xiàn)代碼
首先簡(jiǎn)單說一下什么是行壓縮圖,其實(shí)嚴(yán)格意義上應(yīng)該是行壓縮矩陣2013-05-05

