Re: [問題] 我這樣很爛嗎?
※ 引述《uranusjr (←這人是超級笨蛋)》之銘言:
: 這個是 C 或 C++ style 的問題
: 在 C++ 好像是推薦用 0 而非 NULL, 不過我忘了出處
: 好像 C 裡面 NULL 沒有規定一定是 0, 但是 C++ 有?(不確定)
→
09/03 19:53,
09/03 19:53
推
09/03 20:12,
09/03 20:12
→
09/03 20:13,
09/03 20:13
→
09/03 20:13,
09/03 20:13
去查了
http://en.wikipedia.org/wiki/C%2B%2B11#Null_pointer_constant
C++ didn't adopt the same behavior [of using preprocessor macro NULL],
allowing only 0 as a null pointer constant.
NULL 是 C 裡面為了區別 0 有空指標和「零」這個數字的雙重身份而做出來的巨集
但 C++ 沒有使用這個的習慣, 而是兩個狀況都直接用 0
C++11 的 nullptr 就是為了解決這個問題才出現的
http://www.stroustrup.com/bs_faq2.html#null
Bjarne Stroustrup 的看法應該夠有分量吧XD
In C++, the definition of NULL is 0.
I prefer to avoid macros, so I use 0.
Another problem with NULL is that people sometimes mistakenly believe that
it is different from 0 and/or not an integer.
維基百科的頁面有稍微詳細的解釋
在 C 中, NULL 有時會被展開為 ((void *)0), 不是 int 而是 void *
但是 C++ 標準並不是這樣規定, 而是把 NULL 直接展開為 0
所以用 NULL 和 0 的作用完全相同
但不熟悉的人可能會誤以為 C++ 中看到這個 macro 也可以視為指標
由於 C++ 有函式重載, 這個誤解反而會造成問題
void foo(char *);
void foo(int);
即使使用 NULL, 在 C++ 中還是會使用第二個, 而不是第一個
所以與其造成這個誤會, 不如直接都用 0 比較好
當然如果有 nullptr 的話就要用
--
Les grandes et les meilleurs tone from "Zadok the Priest"
Eine grosse stattliche Veranstaltung by F. Handel
THE MAIN EVENT! These are the men
Sie sind die Besten
"Champions League" by Tony Britten THESE ARE THE CHAMPIONS!
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 114.32.81.146
→
09/03 21:05, , 1F
09/03 21:05, 1F
推
09/03 21:12, , 2F
09/03 21:12, 2F
→
09/03 21:12, , 3F
09/03 21:12, 3F
→
09/03 21:13, , 4F
09/03 21:13, 4F
→
09/03 21:14, , 5F
09/03 21:14, 5F
推
09/03 21:20, , 6F
09/03 21:20, 6F
→
09/03 21:20, , 7F
09/03 21:20, 7F
→
09/03 21:21, , 8F
09/03 21:21, 8F
→
09/03 22:12, , 9F
09/03 22:12, 9F
討論串 (同標題文章)
本文引述了以下文章的的內容:
問題
1
8
完整討論串 (本文為第 5 之 10 篇):
問題
9
29
問題
29
84
問題
7
12
問題
1
8
問題
2
9
問題
3
5
問題
2
6
問題
1
1
問題
4
6