Re: [問題] 關於typedef
先不說把 * 藏起來的問題。
如果你在寫 C++ 的話,
這類 typedef 其實是一個非常麻煩的東西。
因為...
---------------------------------------------------
// node.hxx
typedef struct node {
int value;
struct node *prev;
struct node *next;
} Node;
void NodeAPI1(Node *);
void NodeAPI2(Node *, Node *);
...
---------------------------------------------------
// node_client.hxx
struct Node;
void foo(Node *);
...
---------------------------------------------------
// node_client.cxx
#include "node.hxx"
#include "node_client.hxx"
void foo(Node *n)
{
// 這邊會存取到 n->value 等內部欄位
}
...
---------------------------------------------------
編譯後得到錯誤訊息:
In file included from node_client.cxx:4:
node_client.hxx:3: error: using typedef-name 'Node' after 'struct'
node.hxx:7: error: 'Node' has a previous declaration here
調換 node_client.cxx 兩行 #include 順序的話:
In file included from node_client.cxx:4:
node.hxx:7: error: conflicting declaration 'typedef struct node Node'
node_client.hxx:3: error: 'struct Node' has a previous declaration as
'struct Node'
一種解決方案是拔掉 node_client.hxx 的前置宣告,
然後讓它直接去 #include "node.hxx"。
但這樣做的話就會增加編譯期相依性,
任何寫到 #include "node_client.hxx" 的檔案,
只要 node.hxx 修改就會被重新編譯。
這也是為什麼 node_client.hxx 一開始會打算放前置宣告的原因。
所以 C++ 其實不是很喜歡這種 typedef 的用法,
typedef 在 C++ 一般比較常被用在 template 相關的 code 上。
一開始的寫法其實用純 C compiler 可以編譯通過,
不過 GCC 4.5 開始能用 gcc -Wc++-compat 得到警告訊息:
In file included from node_client.c:4:0:
node_client.hxx:3:8: warning: using 'Node' as both a typedef and a tag is
invalid in C++
node.hxx:7:3: note: originally defined here
--
Ling-hua Tseng (uranus@tinlans.org)
Department of Computer Science, National Tsing-Hua University
Interesting: C++, Compiler, PL/PD, OS, VM, Large-scale software design
Researching: Software pipelining for VLIW architectures
Homepage: http://www.tinlans.org
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 118.160.107.243
※ 編輯: tinlans 來自: 118.160.107.243 (10/04 21:18)
推
10/04 21:30, , 1F
10/04 21:30, 1F
→
10/04 21:31, , 2F
10/04 21:31, 2F
→
10/04 21:33, , 3F
10/04 21:33, 3F
→
10/04 21:34, , 4F
10/04 21:34, 4F
推
10/04 21:39, , 5F
10/04 21:39, 5F
→
10/04 21:41, , 6F
10/04 21:41, 6F
→
10/04 21:41, , 7F
10/04 21:41, 7F
→
10/04 21:43, , 8F
10/04 21:43, 8F
→
10/04 21:44, , 9F
10/04 21:44, 9F
→
10/04 21:45, , 10F
10/04 21:45, 10F
→
10/04 21:47, , 11F
10/04 21:47, 11F
→
10/04 21:48, , 12F
10/04 21:48, 12F
→
10/04 21:52, , 13F
10/04 21:52, 13F
→
10/04 21:52, , 14F
10/04 21:52, 14F
→
10/04 21:53, , 15F
10/04 21:53, 15F
推
10/04 21:53, , 16F
10/04 21:53, 16F
→
10/04 21:54, , 17F
10/04 21:54, 17F
→
10/04 21:54, , 18F
10/04 21:54, 18F
→
10/04 21:55, , 19F
10/04 21:55, 19F
→
10/04 21:55, , 20F
10/04 21:55, 20F
→
10/04 21:56, , 21F
10/04 21:56, 21F
推
10/04 22:06, , 22F
10/04 22:06, 22F
→
10/04 22:07, , 23F
10/04 22:07, 23F
→
10/04 22:07, , 24F
10/04 22:07, 24F
→
10/04 22:08, , 25F
10/04 22:08, 25F
→
10/04 22:09, , 26F
10/04 22:09, 26F
→
10/04 22:10, , 27F
10/04 22:10, 27F
→
10/04 22:10, , 28F
10/04 22:10, 28F
→
10/04 22:11, , 29F
10/04 22:11, 29F
推
10/04 22:33, , 30F
10/04 22:33, 30F
→
10/04 22:58, , 31F
10/04 22:58, 31F
推
10/04 23:02, , 32F
10/04 23:02, 32F
推
10/05 08:58, , 33F
10/05 08:58, 33F
→
10/05 12:09, , 34F
10/05 12:09, 34F
討論串 (同標題文章)