看板Programming
标 题Re: 请教常数建构子
发信站无名小站 (Thu Sep 13 20:35:51 2007)
转信站ptt!ctu-reader!ctu-peer!news.nctu!csnews.cs.nctu!news.cs.nctu!wretch
※ 引述《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海