编程语言
首页 > 编程语言> > C++ 起别名

C++ 起别名

作者:互联网

可以基于typedef、using等关键词实现

typedef 	std::vector<int> intvec;
using 	intvec	= std::vector<int>;	//这两个写法是等价的

另一个例子,函数指针:

typedef void (*FP) (int, const std::string&);
using FP = void (*) (int, const std::string&);

typedef std::string (* fooMemFnPtr) (const std::string&);
    using fooMemFnPtr = std::string (*) (const std::string&);

模板别名:

template <typename T>
using Vec = MyVector<T, MyAlloc<T>>;
 // usage
Vec<int> vec;

或者

template <typename T>
struct Vec
{
  typedef MyVector<T, MyAlloc<T>> type;
};
 // usage
Vec<int>::type vec;

可见,使用using关键词使得语法更简洁。

参考

https://blog.csdn.net/weixin_39640298/article/details/84641726

标签:std,typedef,const,string,别名,C++,using,Vec
来源: https://www.cnblogs.com/Higgerw/p/16595824.html