typedef 的更復雜用法
typedef
宣告與普通變數和函式宣告具有相同語法的規則可用於讀取和寫入更復雜的宣告。
void (*f)(int); // f has type "pointer to function of int returning void"
typedef void (*f)(int); // f is an alias for "pointer to function of int returning void"
這對於具有混淆語法的構造尤其有用,例如指向非靜態成員的指標。
void (Foo::*pmf)(int); // pmf has type "pointer to member function of Foo taking int
// and returning void"
typedef void (Foo::*pmf)(int); // pmf is an alias for "pointer to member function of Foo
// taking int and returning void"
很難記住以下函式宣告的語法,即使對於有經驗的程式設計師也是如此:
void (Foo::*Foo::f(const char*))(int);
int (&g())[100];
typedef
可用於使它們更易於讀寫:
typedef void (Foo::pmf)(int); // pmf is a pointer to member function type
pmf Foo::f(const char*); // f is a member function of Foo
typedef int (&ra)[100]; // ra means "reference to array of 100 ints"
ra g(); // g returns reference to array of 100 ints