精華區beta Programming 關於我們 聯絡資訊
※ 引述《previewslave@kkcity.com.tw ( )》之銘言: > ※ 引述《UNARY.bbs@bbs.cse.ttu.edu.tw (Rootkit Researcher)》之銘言: > > copy assignment operator?? > > "CBox box2 = box1;" 這個是呼叫 box2 的 (copy) constructor 吧? > 正確來說,是呼叫 CBox::CBox(CBox &) 這個建構子,不知道專業術語怎麼說。 > 因為 copy ctor 可以是 > CBox::CBox(CBox *) 或 CBox::CBox(CBox) 兩者應該也可以是 copy ctor 吧。 不是,只有 CBox::CBox(CBox &) 叫做 copy constructor。 而 CBox::CBox(CBox *) 只是單純的 constructor, 又因為它只有一個參數且非 copy constructor, 宣告式前面沒標 explicit 的話會有人稱之為 type conversion constructor, 但因為某些因素, 所以傳 pointer 比較沒有人這樣叫, 除非 class 本身是 smart pointer 之類的。 CBox::CBox(CBox) 不是 copy constructor 這件事, 通常是真正有在寫程式或書看得夠多的人才會知道, 這樣寫甚至會讓 compiler 出現 error 告訴你應該寫成 CBox::CBox(CBox &)。 事實上因為你用 pass-by-value 傳入, 如果 CBox::CBox(CBox) 是合法的 copy constructor, 那這個動作又會呼叫 copy constructor 製作一份副本, 結果又倒回 CBox::CBox(CBox), 然後又再次因為 pass-by-value 呼叫 CBox::CBox(CBox), 不斷 CBox::CBox(CBox) -> CBox::CBox(CBox) -> CBox::CBox(CBox) 這樣 call 下去, 讓這種程式通過編譯就會造成無窮迴圈, compiler 要 inline 的話也無從 inline 起, 所以規定上是直接給你 error 的。 -- 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-224-108.dynamic.hinet.net ─╨─╨─ KGBBS 遨翔"BBS"的狂狷不馴;屬於年少的輕狂色彩 [修改]tinlans:61-230-224-108.dynamic.hinet.net 06/10/28 5:42:14