<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>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                為什么operator=返回一個`reference to *this `? 為了實現連續賦值,賦值操作符必須返回一個引用指向操作符的左側實參。這是你為class實現賦值操作符必須遵循的協議。這個協議不僅適用于標準的賦值形式,也適用于+=、-=、*=等等。 注意,這只是一個協議,并無請執行。如果不遵循它,可能代碼一樣通過編譯。然而這份協議被所有內置類型和標準程序庫所提供的類型如string、vector等所遵守。因此除非你有一個標新立異的好理由,不然還是隨眾吧。 --- 如果沒有重載賦值運算符,編譯器會自動創建默認的賦值運算符重載函數。行為類似默認拷貝構造,進行簡單值拷貝。 --- ~~~ #include <iostream> using namespace std; class Student { public: Student(const char *name) { pName = new char[strlen(name) + 1]; strcpy(pName, name); } //防止淺拷貝 Student(const Student &stu) { cout << "拷貝構造" << endl; pName = new char[strlen(stu.pName) + 1]; strcpy(pName, stu.pName); } //重寫賦值運算符重載 Student &operator=(const Student &stu) { //不確定this->pName的空間是否能裝下stu中數據,所以先釋放 if(this->pName != NULL) { delete[] this->pName; this->pName = NULL; } //申請堆區空間,大小由stu決定 this->pName = new char[strlen(stu.pName) + 1]; //拷貝數據 strcpy(this->pName, stu.pName); //返回對象本身 return *this; } ~Student() { if (pName != NULL) { delete[] pName; pName = NULL; } } void printStudent() { cout << "Name: " << pName << endl; } public: char *pName; }; void test02() { Student s1("x"); Student s2("jd"); s1.printStudent(); s2.printStudent(); s1 = s2; s1.printStudent(); } int main() { test02(); getchar(); return EXIT_SUCCESS; } ~~~
                  <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>

                              哎呀哎呀视频在线观看