我很想知道 typedef 究竟是如何工作的。
typedef struct example identifier;
identifier x;
在上面的语句中,'identifier' 只是在代码中用 'struct example' 替换(类似于字符串替换)吗?如果不是,typedef 在这里做什么?
请指教!
最佳答案
不,这不是字符串替换——那是宏。它为该类型创建一个别名。
typedefs are preferred over macros对于自定义类型,部分原因是它们可以正确编码指针类型。
typedef char *String_t;
#define String_d char *
String_t s1, s2;
String_d s3, s4;
s1, s2, and s3 are all declared as char *, but s4 is declared as a char, which is probably not the intention.
关于c++ - typedef 只是代码中的字符串替换还是其他内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3263252/