[問題] 一個 typedef 語法

看板C_and_CPP作者 (「雄辯是銀,沉默是金」)時間12年前 (2013/12/11 21:59), 編輯推噓6(6031)
留言37則, 2人參與, 最新討論串1/4 (看更多)
typedef int P(); typedef int Q(); class X { static P(Q); // equivalent static int Q() }; 實在不懂 P(Q) 在括號裡可以放個 Q 嗎? 不過 compile 是會過的。 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 58.114.140.230

12/11 22:26, , 1F
那句話的註解我懷疑是不是正確的說 XD
12/11 22:26, 1F
我把出處列出來好了 The design and evolution of c++ (簡體中文版) p126 原文版本 141~142 ※ 編輯: descent 來自: 58.114.140.230 (12/11 22:31)

12/11 22:36, , 2F
簡單說, 加一列 static int Q();跟類似宣告,去編看看就知道了.
12/11 22:36, 2F

12/11 22:38, , 3F
static int Q() 跟 static int Q(void) ==> error
12/11 22:38, 3F

12/11 22:38, , 4F
static int Q(int) ==> pass
12/11 22:38, 4F
#include <iostream> #include <typeinfo> using namespace std; typedef int P(); typedef int Q(); class X { static int Q(); static int Q(int); }; int main(int argc, char *argv[]) { return 0; } 都會過, 我猜你不是放在 class X 裡頭。

12/11 22:38, , 5F
我懷疑是不是我的能力也有問題了 Orz , 用 Q 定義一個函式
12/11 22:38, 5F

12/11 22:39, , 6F
應是用 Q func{ return 0;} 寫下 static P(Q){...} 噴 err
12/11 22:39, 6F

12/11 22:40, , 7F
沒誤會的話,vc上err指的是不能傳遞一個函式,而要函式指標
12/11 22:40, 7F
我用 typeid 確定 P 是 function, 不過這樣的宣告法, 完全不懂其意思。 ※ 編輯: descent 來自: 58.114.140.230 (12/11 23:00)

12/12 00:23, , 8F
我是放在 class X 裏頭沒錯呀. 如果將 Q(int) 換為 Q(void)
12/12 00:23, 8F

12/12 00:24, , 9F
編譯時會出現
12/12 00:24, 9F

12/12 00:24, , 10F
11:13: error: 'static int X::Q()' cannot be overloaded
12/12 00:24, 10F

12/12 00:24, , 11F
10:11: error: with 'static int X::Q()'
12/12 00:24, 11F

12/12 00:25, , 12F
我是使用 g++ 4.6.3 .
12/12 00:25, 12F

12/12 00:29, , 13F
再試 g++ 4.2.4 及 g++ 3.3.4 也相同情況,
12/12 00:29, 13F

12/12 00:30, , 14F
3.3.4 的訊息稍有不同:
12/12 00:30, 14F

12/12 00:30, , 15F
error: `static int X::Q()' and `static int X::Q()' cannot
12/12 00:30, 15F

12/12 00:30, , 16F
overloaded
12/12 00:30, 16F

12/12 00:31, , 17F
9: warning: all member functions in class `X' are private
12/12 00:31, 17F

12/12 00:32, , 18F
但基本上 error 的意思一樣 (error: ... ==> 11: error: ...)
12/12 00:32, 18F

12/12 00:36, , 19F
如果 Q(int) 下移兩列, 就出問題了, 編譯時會出現
12/12 00:36, 19F

12/12 00:37, , 20F
12:17: error: 'int Q(int)' redeclared as different kind of
12/12 00:37, 20F

12/12 00:37, , 21F
symbol
12/12 00:37, 21F

12/12 00:37, , 22F
6:13: error: previous declaration of 'typedef int Q()'
12/12 00:37, 22F

12/12 00:38, , 23F
不放 class X 裏頭根本就有問題呀.
12/12 00:38, 23F

12/12 00:40, , 24F
剛沒注意. Class X 中, static int Q(); 我是寫 static P(Q);
12/12 00:40, 24F

12/12 00:44, , 25F
然後空兩格的部分我是直接按一個 <TAB>.
12/12 00:44, 25F

12/12 00:45, , 26F
也就是 line 8-12:
12/12 00:45, 26F

12/12 00:45, , 27F
class X
12/12 00:45, 27F

12/12 00:46, , 28F
{
12/12 00:46, 28F

12/12 00:46, , 29F
static P(Q);
12/12 00:46, 29F

12/12 00:46, , 30F
static int Q(void);
12/12 00:46, 30F

12/12 00:46, , 31F
};
12/12 00:46, 31F

12/12 00:47, , 32F
其中的 Q(void) 換為 Q(int) 時, 編輯才會過.
12/12 00:47, 32F

12/12 00:49, , 33F
另外 typedef int Q(); 沒寫也沒差.
12/12 00:49, 33F

12/12 01:10, , 34F
去找了一下那段 code, 它有下一句註解:
12/12 01:10, 34F

12/12 01:11, , 35F
// the parentheses around Q are redundant
12/12 01:11, 35F

12/12 01:13, , 36F
意思 static P(Q); 就是 static P Q; 啊.
12/12 01:13, 36F

12/12 01:18, , 37F
現在才發現, 原來後面有人回文了 @@
12/12 01:18, 37F
文章代碼(AID): #1Ig6_73h (C_and_CPP)
文章代碼(AID): #1Ig6_73h (C_and_CPP)