模板引數 typedef,or macro

2021-05-22 10:24:50 字數 606 閱讀 9801

typedef 是乙個聰明的孩子,他總是按語義翻譯:

typedef  char*  sptr;

那麼當如下形式定義變數p,q時,

sptr p,q;  //等價於char *p, *q;

定義的p,q都是char*的指標變數

相比之下,macro就不求甚解,它總是死板硬套

#define char* sptr;

sptr p,q;//等價於char  *p,q;

則定義的變數p是char*的指標變數,但是q就不是了,它只是char型別的字元變數。

那麼模板引數究竟如何呢?很可惜,他更像是macro的種,不過卻可以輕鬆質變

template< typename t >

class a

t p,q;

};當你傳入char*給模板形參t的時候,得到變數p,q就形如char*p,q;所以預設行為下與macro相同

不過也可以輕易的改變過來

template< typename t >

typedef t  type;//實現脫胎換骨的點

type p,q;

};typedef t type;訪問屬性設為private,從而對外部使用者透明。

C 模板模板引數

c 模板的使用一共有以下幾種情況。而本篇介紹模板模板引數。模板引數就是模板的引數,我們一般指定為t型別,實際上可以使用任何的名字,例如指定乙個foo的模板引數 temlate foo calc const foo a,const foo b 而模板模板引數則是模板的引數又是乙個模板,例如 templ...

C 模板模板引數

模板引數就是模板的引數,我們一般指定為t型別,實際上可以使用任何的名字,例如指定乙個foo的模板引數 temlate foo calc const foo a,const foo b 而模板模板引數則是模板的引數又是乙個模板,例如 templatetypename container class x...

可變參模板模板模板引數

include include include include include include include include using namespace std namespace nmsp1 classa template typename.args class myclasst 主模板 t...