C++11新特性-使用別名(alias)

傳統

傳統使用別名的方法是用typedef關鍵字。如:spa

typedef double wages;
typedef wages base, *p;

C++11引入了一種新的使用別名的方式,叫做:別名聲明(alias declaration)指針

using SI = Sales_item;

typedef的缺陷

typedef char *pstring;    //pstring是char*的別名
const pstring cstr = 0;   //cstr is a constant pointer to char. const指針,而不是指向const對象的指針
const pstring *ps;        //ps is a pointer to constant pointer to char

不能將cstr簡單的用char*來替換:code

const char *cstr = 0;     //這樣理解cstr是不對的,pstring基本類型是指針類型,而這裏基本類型是const char

若是用using pstring = char *; 來代替typedef,能夠減小這種誤解。這樣能夠明顯的提醒你們pstring是char指針類型。對象

相關文章
相關標籤/搜索