最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++中this指針的介紹及使用實例講解

 更新時間:2025年05月08日 17:03:18   作者:秋風(fēng)&蕭瑟  
這篇文章主要介紹了C++中this指針的介紹及使用實例講解,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧

this指針的介紹及使用

1.this指針的作用

Cat &cmpAge(Cat &other)
{
	if(other.age>age)
	    return other;
	else
	    return *this;
}

用這個例子引出this指針:
指向當(dāng)前對象的一個指針,哪個對象調(diào)用成員函數(shù),this指針就指向該對象

示例代碼1:(this指針存放當(dāng)前對象的地址)

#include <iostream>
using namespace std;
/*
    引入this指針:C++專門用來指向當(dāng)前對象地址的一個指針
                  當(dāng)前對象是誰,this指針就自動存放了誰的地址
    當(dāng)前對象:誰調(diào)用了成員函數(shù),誰就是當(dāng)前對象
*/
class Rect
{
public:
    /*
        底層原理:
          當(dāng)前對象.show();
          Rect *this=&當(dāng)前對象 
    */
    void show()
    {
        cout<<"this指針打印出來的地址是當(dāng)前對象的地址: "<<this<<endl;
    }
};
int main(int argc,char **argv)
{
    //創(chuàng)建矩形類的對象
    Rect r1;
    Rect r2;
    cout<<"r1的地址: "<<&r1<<endl;
    cout<<"r2的地址: "<<&r2<<endl;
    //當(dāng)前對象:誰(r1)調(diào)用了成員函數(shù),誰(r1)就是當(dāng)前對象
    r1.show();
    //當(dāng)前對象:誰(r2)調(diào)用了成員函數(shù),誰(r2)就是當(dāng)前對象
    r2.show();
}
/*
執(zhí)行結(jié)果:
	r1的地址: 0x7ffdf90a5cb6
	r2的地址: 0x7ffdf90a5cb7
	this指針打印出來的地址是當(dāng)前對象的地址: 0x7ffdf90a5cb6
	this指針打印出來的地址是當(dāng)前對象的地址: 0x7ffdf90a5cb7
*/

示例代碼2:(this指針的使用)

#include <iostream>
using namespace std;
/*
    引入this指針:C++專門用來指向當(dāng)前對象地址的一個指針
                  當(dāng)前對象是誰,this指針就自動存放了誰的地址
    定義方法:比較兩個矩形對象的大小(按照w和h比較,要求w,h都同時大于另外一個矩形),返回較大的那個對象
*/
class Rect
{
public:
    //定義方法給w,h設(shè)置值,間接地使用w和h
    void setAttr(float _w,float _h);
    //比較兩個矩形的大小
    Rect compare(Rect &other)
    {
        if((this->w)>other.w && (this->h)>other.h)
        {
            return *this;
        }
        else
            return other;
    }
    void show()
    {
        cout<<"寬: "<<w<<endl;
        cout<<"高: "<<h<<endl;
    }
private:
    //屬性
    float w;  
    float h; 
};
void Rect::setAttr(float _w,float _h)
{
    w=_w;
    h=_h;
}
int main(int argc,char **argv)
{
    //創(chuàng)建矩形類的對象
    Rect r1;
    Rect r2;
    //設(shè)置寬高
    r1.setAttr(9.8,5.6);
    r2.setAttr(1.2,0.5);
    //比較大小
    //寫法1:當(dāng)前對象就是r1
    //Rect temp=r1.compare(r2);
    //temp.show();
    //寫法2:當(dāng)前對象就是r2
    Rect temp=r2.compare(r1);
    temp.show();
}
/*
執(zhí)行結(jié)果:
    寬: 9.8
    高: 5.6
*/ 

2.this指針的寫法

this->age //指針調(diào)用
(*this).age //this解引用

示例代碼:

#include <iostream>
using namespace std;
/*
    this指針平時寫成員函數(shù)代碼,可以省略的
*/
class Rect
{
public:
    void show()
    {
    }
    void setAttr(float _w,float _h)
    {
        //寫法1:標(biāo)準(zhǔn)的寫法-->寫全面
        //this->w=_w;
        //this->h=_h;
        //寫法2:省略this的寫法
        w=_w;
        h=_h;
    }
private:
    float w;
    float h;
};
int main(int argc,char **argv)
{
    Rect r1;
    Rect r2;
    r1.setAttr(1.2,0.8);
}

到此這篇關(guān)于C++中this指針的介紹及使用的文章就介紹到這了,更多相關(guān)C++ this指針內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論

红安县| 阜城县| 荣成市| 延吉市| 镇坪县| 陵川县| 克东县| 竹山县| 周至县| 辽源市| 利辛县| 庆云县| 红安县| 丰县| 许昌市| 昂仁县| 凌海市| 鄂伦春自治旗| 闵行区| 巴东县| 东莞市| 昌宁县| 喀什市| 察哈| 栾城县| 大宁县| 汾阳市| 桂林市| 江永县| 新郑市| 会昌县| 绍兴市| 柘荣县| 临漳县| 马关县| 临湘市| 黑山县| 上栗县| 兴仁县| 宁波市| 平陆县|