Re: 請教c++的this傳回值

看板Programming作者時間18年前 (2006/11/12 02:32), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串3/4 (看更多)
※ 引述《jiannrong@kkcity.com.tw ( )》之銘言: > 因不知該如何顯示出來,所以在這裡詢問,麻煩大家幫忙 > c++可以有this與*this,而我現在想了解這兩種的傳回值 > 不知該怎麼寫 this 只是一個 pointer, pointer 只是一種 variable, 所以根本就沒有傳回值這種說法, 頂多只能說內容值, 運算式的結果就單純只是運算結果, 除非在談 operator overloading, 不然幾乎不會有人用運算式的傳回值這種怪異說法, 事實上就算在談 operator overloading, 那也不過是在講運算子的傳回值, 而不是講運算式的傳回值, 即使運算子的傳回值恰好為運算式的結果亦然。 this 只會出現在 object 的 non-static method 裡面, 是哪個 object 正在執行那個 method, 那 this 就指向哪個 object, 換句話說 this 的內容值就是該 object 的 start address。 this 和 *this 是兩種不同的運算式, 並不是什麼可以有 this 與 *this 這樣子講, 在 C 和 C++ 把 * 這個稱作 dereference operator 的運算子, 放在一個 pointer 前面構成一個運算式, 是非常理所當然的, 這是因為它是一元運算子; 而它的運算結果當然就是它指向的物件本身。 不管是發問或是和人交流討論, 如果基本術語上的用詞不能嚴謹, 這樣會有很多人看不懂你在問什麼, 看得懂的也會因為你用詞怪異, 覺得你連基本名詞都搞不清楚(可能是連書都沒好好看過就開始寫程式), 那教了也有很大的可能是白教, 所以就不想回你的問題。 > 附上這兩個程式碼,麻煩各位點破我的迷思,謝謝 > #include <iostream> > #include <cstdlib> > using namespace std; > class Cal > { > int x; > public: > Cal(int n) { x = n; } //建立者函數 > Cal calcSum(Cal obj) { //加法運算 > x += obj.x; //x = x + obj.x 看你搞不太懂 this 的意思, 所以順帶跟你說一下上面這行敘述等價於: this->x += obj.x; 和 (*this).x += obj.x; 假設你正執行到 main() 中的 c = a.calcSum(b), 那麼 this 指向的 object 就是 a, 而 *this 就是 a 本身。 > return *this; //傳回(*this).x 從註解可以看出你的觀念有問題, 你明明寫 return *this; 為什麼會認為是傳回了 (*this).x 呢? return *this 很顯然就是傳回 *this 的意思不是嗎? 或是說, 你下這註解的意思是希望改成能達到傳回 (*this).x 的值這種目的? 其實這也很簡單, 你把 calcSum() 這個 method 的傳回值型別改成跟 Cal::x 相同不就好了, 只不過你下面 c = a.calcSum(b) 這條算式就得做修改, 你不能再用 c 這個 object 去接傳回值。 > } > }; > int main() { > Cal a(100), b(200), c(0); //定義 a.x=100, b.x=200 > c = a.calcSum(b); //計算 c.x = a.x + b.x > // cout<<"c :"<<c<<endl; //如何顯示出 (*this).x ?? 上面說過, this 只有在 non-static method 中才能被使用, 而唯有透過 object, 才有辦法執行 non-static method, 既然你的 main() 並不在 class 裡面, 它顯然也並未透過任何 object 來被執行, 那麼在 main() 的程式碼中又何來 this 之有? 還是說, 你的意思只是想把 c.x 的內容值印出來? 這樣的話那標題就跟內容無關了, 因為你標題問的是跟 this 有關的東西, 所以我也針對跟 this 有關的部分回答你。 > system("PAUSE"); > return 0; //程式正常結束 > } > 第二個程式 > #include <iostream> > #include <cstdlib> > using namespace std; > class Cal > { > int x; > public: > Cal(int n) { x = n; } //建立者函數 > Cal *calcSum(Cal obj) { //加法運算 > x += obj.x; //x = x + obj.x > return this; //傳回this->x 這個叫做傳回 this。 > } > }; > int main() { > Cal a(100), b(200), c(0); //定義 a.x=100, b.x=200 > c = a.calcSum(b); //計算 c.x = a.x + b.x > // cout<<"c :"<<c<<endl; //如何顯示出 this->x ?? 在 main 裡面沒有 this 這種東西。 > system("PAUSE"); > return 0; //程式正常結束 > } 回到你一開頭的主題, 你說你寫這些程式的目的是為了瞭解 this 和 *this 的差別, 可是不知道該怎麼寫, 這其實非常簡單: #include <iostream> using namespace std; class Test { public: Test() : val(0) { } Test(int v) : val(v) { } // 用來幫助瞭解 this void doTest1(Test *ptr) { if(ptr == this) cout << "ptr == this" << endl; else cout << "ptr != this" << endl; // 附註:在 C++ 這時最好用 reinterpret_cast<void *>() 來轉 cout << "ptr: " << (void *)ptr << endl; cout << "this: " << (void *)this << endl; } // 用來幫助瞭解 *this bool doTest2() { cout << (*this).val << endl; } // 暫時不用 private 方便理解 // private: int val; }; int main() { Test a(1), b(2), c(3); // 會發現執行 a.doTest1() 時 Test::doTest1() 中的 this 恆等於 &a, // 將 a.doTest1() 全部換成 b.doTest1() 或 c.doTest1() 再觀察看看 a.doTest1(&a); a.doTest1(&b); a.doTest1(&c); // 可發現執行 a.doTest2() 時 Test::doTest2() 中的 *this 就是 a, // 而執行 b.doTest2() 時 Test::doTest2() 中的 *this 就是 b, // 以此類推 a.doTest2(); cout << a.val << endl; b.doTest2(); cout << b.val << endl; c.doTest2(); cout << c.val << endl; return 0; } -- Name: Tseng, Ling-hua E-mail Address: uranus@it.muds.net School: National Tsing Hua University Department: Computer Science Interesting: C++, Compiler, PL/PD, OS, VM, Large-scale software design Researching: Undecided Homepage: https://it.muds.net/~uranus -- ╔═══╗ ┼────────────────────────╮ 狂狷 Origin:[ 狂 狷 年 少 ] whshs.cs.nccu.edu.tw ╰─╮ 年少 ┼╮ < IP:140.119.164.252 > ╰─╮ ╚╦═╦╝ From:61-230-234-91.dynamic.hinet.net ─╨─╨─ KGBBS 遨翔"BBS"的狂狷不馴;屬於年少的輕狂色彩 [修改]tinlans:61-230-234-91.dynamic.hinet.net 06/11/12 2:19:21
文章代碼(AID): #15LXSa00 (Programming)
文章代碼(AID): #15LXSa00 (Programming)