Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

有 c++ 基本問題想問...

0 views
Skip to first unread message

流失..

unread,
Mar 2, 2000, 3:00:00 AM3/2/00
to
※ 引述《maninmi...@bbs.csie.nctu.edu.tw (canda)》之銘言:
> void main()
> {
> int a =9;
> print(a);
> }
> void print(const int& x)
> { ^^^^^^ ------>這裡宣告 const, 有必要嗎 ?
> cout << x...
> }
> 若不加 const, 會有影響嗎 ?

有影響.. 加了const.. x不能被更動..
--
※ Origin: 楓橋驛站<bbs.cs.nthu.edu.tw> ◆ From: n197.n203-107-5.eranet.net

哈哈哈

unread,
Mar 2, 2000, 3:00:00 AM3/2/00
to
※ 引述《ldc...@bbs.cs.nthu.edu.tw (流失..)》之銘言:

> ※ 引述《maninmi...@bbs.csie.nctu.edu.tw (canda)》之銘言:
> > void main()
> > {
> > int a =9;
> > print(a);
> > }
> > void print(const int& x)
> > { ^^^^^^ ------>這裡宣告 const, 有必要嗎 ?
> > cout << x...
> > }
> > 若不加 const, 會有影響嗎 ?
> 有影響.. 加了const.. x不能被更動..


補充:可以讓記憶體最佳化..............................
.............................................................
.............................................................
--
[1;32m※ Origin: [33m晨曦之鄉 [37m<bbs.cyut.edu.tw> [m
[1;31m◆ From: [36mM413-59.cyut.edu.tw [m

jon

unread,
Mar 2, 2000, 3:00:00 AM3/2/00
to
※ 引述《koop...@bbs.cyut.edu.tw (哈哈哈)》之銘言:
> ※ 引述《ldc...@bbs.cs.nthu.edu.tw (流失..)》之銘言:

> > 有影響.. 加了const.. x不能被更動..
> 補充:可以讓記憶體最佳化..............................

const和效能並沒有一定的關係...更別說記憶體最佳化了...
const的目的在type safe, 程式的正確性...
--
[1;32m※ Origin: [33m交大資工鳳凰城資訊站 [37m<bbs.csie.nctu.edu.tw> [m
[1;31m◆ From: [36msj8-172.dialup.seed.net.tw [m

櫻同學

unread,
Mar 2, 2000, 3:00:00 AM3/2/00
to
這題重點應該是,
" 函式只是 print x, 照說任何情況x都不會更改,
但為何這裡用 const ? "

" 也就是說, 在甚摸情況下, x 會被更改, 所以需要 const" !

※ 引述《ldc...@bbs.cs.nthu.edu.tw (流失..)》之銘言:
> ※ 引述《maninmi...@bbs.csie.nctu.edu.tw (canda)》之銘言:
> > void main()
> > {
> > int a =9;
> > print(a);
> > }
> > void print(const int& x)
> > { ^^^^^^ ------>這裡宣告 const, 有必要嗎 ?
> > cout << x...
> > }
> > 若不加 const, 會有影響嗎 ?
> 有影響.. 加了const.. x不能被更動..

--
[1;32m※ Origin: [33m交大資工鳳凰城資訊站 [37m<bbs.csie.nctu.edu.tw> [m

[1;31m◆ From: [36mtrt-on63-74.netcom.ca [m

難得的恐龍

unread,
Mar 2, 2000, 3:00:00 AM3/2/00
to
※ 引述《freak...@bbs.csie.nctu.edu.tw (jon)》之銘言:
> ※ 引述《koop...@bbs.cyut.edu.tw (哈哈哈)》之銘言:

> > 補充:可以讓記憶體最佳化..............................
> const和效能並沒有一定的關係...更別說記憶體最佳化了...
> const的目的在type safe, 程式的正確性...


� Are you sure ? Can anyone explain it ?
But I think "const" somewhat helps to
optimum the memory layout of some variable
declared as const in compile phase .

--
[1;33m※ Origin: [36m奇摩 大摩域 [37m<telnet://bbs.kimo.com.tw> [m
[1;35m◆ From: [1;32m210.63.66.73 [m

哈哈!我要田中麗奈!

unread,
Mar 3, 2000, 3:00:00 AM3/3/00
to
※ 引述《rosefo...@bbs.kimo.com.tw (難得的恐龍)》之銘言:
> ※ 引述《freak...@bbs.csie.nctu.edu.tw (jon)》之銘言:

> > const和效能並沒有一定的關係...更別說記憶體最佳化了...
> > const的目的在type safe, 程式的正確性...
> � Are you sure ? Can anyone explain it ?
> But I think "const" somewhat helps to
> optimum the memory layout of some variable
> declared as const in compile phase .
我覺ㄉ去問compilerㄉ設計者比較快
大家不需要為這個問題傷腦筋
最好ㄉ答案就在compiler身上
--
[1;32m※ Origin: [33m雲淡風清 [37m<bbs.au.edu.tw> [m
[1;31m◆ From: [36m210.60.11.38 [m

哈哈!我要田中麗奈!

unread,
Mar 3, 2000, 3:00:00 AM3/3/00
to
※ 引述《ldc...@bbs.cs.nthu.edu.tw (流失..)》之銘言:
> ※ 引述《maninmi...@bbs.csie.nctu.edu.tw (canda)》之銘言:
> > void main()
> > {
> > int a =9;
> > print(a);
> > }
> > void print(const int& x)
> > { ^^^^^^ ------>這裡宣告 const, 有必要嗎 ?
> > cout << x...
> > }
> > 若不加 const, 會有影響嗎 ?
> 有影響.. 加了const.. x不能被更動..
const可以幫助除錯
如果你寫ㄌ一個程式
你知道你不會把送進來ㄉ指標會參考去做更動
那麼你最好在變數前面加上const
如果你不幸在函式理動到ㄌ這個變數
那麼compiler就根本不給你過

函式小ㄉ時候這一方面是幾乎沒有作用
因為你知道你ㄉ函式在幹麻
但是寫ㄉ比較大ㄌ
函式裡面又有函式..然後又有函式..又有函式...(bbs就是這種東西..)
那麼..的確令人吐寫
這等於是說加一層保險
有,總比沒有好,對吧!?

jon

unread,
Mar 3, 2000, 3:00:00 AM3/3/00
to
※ 引述《rosefo...@bbs.kimo.com.tw (難得的恐龍)》之銘言:
> ※ 引述《freak...@bbs.csie.nctu.edu.tw (jon)》之銘言:
> > const和效能並沒有一定的關係...更別說記憶體最佳化了...
> > const的目的在type safe, 程式的正確性...
> � Are you sure ? Can anyone explain it ?
> But I think "const" somewhat helps to
> optimum the memory layout of some variable
> declared as const in compile phase .

Occasionally, const does help compilers generate more efficient code.
However, that's not the purpose of const, and using const does
not guarantee the compiler would do any optimization.

--
[1;32m※ Origin: [33m交大資工鳳凰城資訊站 [37m<bbs.csie.nctu.edu.tw> [m

[1;31m◆ From: [36mt199-107.dialup.seed.net.tw [m

0 new messages