Re: 請教常數建構子

看板Programming作者時間18年前 (2007/09/13 21:01), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串2/2 (看更多)
※ 引述《jiannrong (大改名片檔)》之銘言: > 請問將建構子宣告為常數,這在程式上有何不同的地方呢? > #include <iostream> > using namespace std; > class people > { > private: > int *height; > public: > void display(); > people(const int *); > ~people(){}; > }; > people:: people(const int *ht) > { int *pt_temp=new int(*ht); > height=pt_temp; > } > void people::display() > {cout<<"高"<<*height<<"address" <<height<<endl; > } > int main() > { > int *hptr=new int(120); > people *Hobbit=new people(hptr); > cout << "Hobbit is"; > Hobbit->display(); > people *abcde=new people(hptr); > cout << "abcde is"; > abcde-> display(); > delete hptr; > system("PAUSE"); > return 0;} 他並沒有將 建構子宣告成常數喔 被宣告成常數的是 傳進去的指標 這樣宣告 就是告訴編譯器 傳進去的指標所指的位置 那個函式並不能去修改他 這是用來保護指標內容用的 -- 我的程式設計網誌 http://blog.yam.com/swwuyam -- 夫兵者不祥之器物或惡之故有道者不處君子居則貴左用兵則貴右兵者不祥之器非君子 之器不得已而用之恬淡為上勝而不美而美之者是樂殺人夫樂殺人者則不可得志於天下 矣吉事尚左凶事尚右偏將軍居左上將軍居右言以喪禮處之殺人之眾以哀悲泣之戰勝以 喪禮處之道常無名樸雖小天下莫能臣侯王若能守之萬物將自賓天地相合以降甘露民莫 之令而自均始制有名名亦既有夫亦將知止知止可以不殆譬道之在天 61.57.152.222
文章代碼(AID): #16wJIF00 (Programming)
討論串 (同標題文章)
文章代碼(AID): #16wJIF00 (Programming)