<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                [TOC] # 原理 通過上例我們知道,c++的數據和操作也是分開存儲,并且每一個非內聯成員函數(non-inline member function)只會誕生一份函數實例,也就是說多個同類型的對象會共用一塊代碼 那么問題是:這一塊代碼是如何區分那個對象調用自己的呢? ![](https://img.kancloud.cn/83/4e/834ea98f82abd2319706ac6f3f8634f5_363x166.png) c++通過提供特殊的對象指針,this指針,解決上述問題。This指針指向被調用的成員函數所屬的對象。 c++規定,this指針是隱含在對象成員函數內的一種指針。當一個對象被創建后,它的每一個成員函數都含有一個系統自動生成的隱含指針this,用以保存這個對象的地址,也就是說雖然我們沒有寫上this指針,編譯器在編譯的時候也是會加上的。因此this也稱為“指向本對象的指針”,this指針并不是對象的一部分,不會影響sizeof(對象)的結果。 this指針是C++實現封裝的一種機制,它將對象和該對象調用的成員函數連接在一起,在外部看來,每一個對象都擁有自己的函數成員。一般情況下,并不寫this,而是讓系統進行默認設置。 > this指針永遠指向當前對象。 成員函數通過this指針即可知道操作的是那個對象的數據。**This指針是一種隱含指針,它隱含于每個類的非靜態成員函數中。**This指針無需定義,直接使用即可。 **注意:靜態成員函數內部沒有this指針,靜態成員函數不能操作非靜態成員變量。** # **c++編譯器對普通成員函數的內部處理** ![](https://img.kancloud.cn/16/92/16922778de82e4a96bffe41524dc52e2_680x353.png) # this指針的使用 * 當形參和成員變量同名時,可用this指針來區分 * 在類的非靜態成員函數中返回對象本身,可使用`return *this.` ~~~ class Person{ public: //1. 當形參名和成員變量名一樣時,this指針可用來區分 Person(string name,int age){ //name = name; //age = age; //輸出錯誤 this->name = name; this->age = age; } //2. 返回對象本身的引用 //重載賦值操作符 //其實也是兩個參數,其中隱藏了一個this指針 Person PersonPlusPerson(Person& person){ string newname = this->name + person.name; int newage = this->age + person.age; Person newperson(newname, newage); return newperson; } void ShowPerson(){ cout << "Name:" << name << " Age:" << age << endl; } public: string name; int age; }; //3. 成員函數和全局函數(Perosn對象相加) Person PersonPlusPerson(Person& p1,Person& p2){ string newname = p1.name + p2.name; int newage = p1.age + p2.age; Person newperson(newname,newage); return newperson; } int main(){ Person person("John",100); person.ShowPerson(); cout << "---------" << endl; Person person1("John",20); Person person2("001", 10); //1.全局函數實現兩個對象相加 Person person3 = PersonPlusPerson(person1, person2); person1.ShowPerson(); person2.ShowPerson(); person3.ShowPerson(); //2. 成員函數實現兩個對象相加 Person person4 = person1.PersonPlusPerson(person2); person4.ShowPerson(); system("pause"); return EXIT_SUCCESS; } ~~~ # 常函數 * 用const修飾的成員函數時,const修飾this指針指向的內存區域,成員函數體內不可以修改本類中的任何普通成員變量, * 當成員變量類型符前用mutable修飾時例外。 ~~~ //const修飾成員函數 class Person{ public: Person(){ this->mAge = 0; this->mID = 0; } //在函數括號后面加上const,修飾成員變量不可修改,除了mutable變量 void sonmeOperate() const{ //this->mAge = 200; //mAge不可修改 this->mID = 10; } void ShowPerson(){ cout << "ID:" << mID << " mAge:" << mAge << endl; } private: int mAge; mutable int mID; }; int main(){ Person person; person.sonmeOperate(); person.ShowPerson(); system("pause"); return EXIT_SUCCESS; } ~~~ # 常對象 * 常對象只能調用const的成員函數 * 常對象可訪問 const 或非 const 數據成員,不能修改,除非成員用mutable修飾 ~~~ class Person{ public: Person(){ this->mAge = 0; this->mID = 0; } void ChangePerson() const{ mAge = 100; mID = 100; } void ShowPerson(){ this->mAge = 1000; cout << "ID:" << this->mID << " Age:" << this->mAge << endl; } public: int mAge; mutable int mID; }; void test(){ const Person person; //1. 可訪問數據成員 cout << "Age:" << person.mAge << endl; //person.mAge = 300; //不可修改 person.mID = 1001; //但是可以修改mutable修飾的成員變量 //2. 只能訪問const修飾的函數 //person.ShowPerson(); person.ChangePerson(); } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看