c++之類型別名的實(shí)現(xiàn)
類型別名,顧名思義就是為一個(gè)類型創(chuàng)建一個(gè)新的名稱,使用這個(gè)新的名稱與使用之前的名稱一模一樣。
c++98版本的類型別名
c++98版本的類型別名方式使用的是typedef關(guān)鍵字,通過typedef關(guān)鍵字實(shí)現(xiàn)的類型別名,下面將展示一些c++98版本的類型別名方式。
//基本類型別名 typedef int Integer; //不要忘記逗號 typedef unsigned int UInt; //不要忘記逗號 //指針類型別名 typedef int* IntPtr; //不要忘記逗號 typedef char* CharPtr; //不要忘記逗號 //函數(shù)指針別名 typedef void (*FuncPtr)(int, int); //復(fù)雜類型別名 typedef std::vector<std::string> StringVector; typedef std::map<std::string, int> StringIntMap;
現(xiàn)代c++的類型別名方式
在c++11版本引入了using,通過using實(shí)現(xiàn)類型別名,詳細(xì)的示例如下所示:
//基本類型的類型別名 using Integer = int; using UInt = unsigned int; //指針類型的類型別名 using IntPtr = int*; using CharPtr = char*; //函數(shù)指針的類型別名 using FuncPtr = void(*)(int, int); //復(fù)雜類型的類型別名 using StringVector = std::vector<std::string>; using StringIntMap = std::map<std::string, int>;
為什么引入using?
之所以引入using作為推薦的類型別名方式,是因?yàn)閡sing方式相對于typedef有著明顯的優(yōu)勢。
- 更清晰的語法
typedef void(*FuncPtr)(int, int); //typedef形式 using FuncPtr = void(*)(int, int); //using形式,更直觀
- 模板別名支持
typedef不支持模板別名,但是using支持模板別名。請看下面的例子:
// 使用using可以創(chuàng)建模板別名
template<typename T>
using Vec = std::vector<T>;
// 使用示例
Vec<int> numbers; // 等價(jià)于 std::vector<int>
Vec<std::string> words; // 等價(jià)于 std::vector<std::string>
// typedef無法直接實(shí)現(xiàn)模板別名,需要額外包裝
template<typename T>
struct VectorTypedef {
typedef std::vector<T> type;
};
常見場景
簡化復(fù)雜類型
很多時(shí)候?qū)懸恍?fù)雜類型的時(shí)候,每次創(chuàng)建該類型的變量或者其他使用該類型的場景,都需要寫很長的類型名,通過類型別名,我們可以定義一個(gè)簡短的名字,后續(xù)使用到該類型時(shí),使用這個(gè)別名即可。請看下面的例子:
//簡化stl容器 using StringSet = std::set<std::string>; using IntMatrix = std::vector<std::vector<int>>; // 簡化智能指針類型 using StringPtr = std::shared_ptr<std::string>; using IntUPtr = std::unique_ptr<int>;
回調(diào)函數(shù)類型
using ErrorCallback = std::function<void(const std::string&)>; using EventHandler = std::function<void(const Event&)>;
在類中使用類型別名
class Container {
public:
using value_type = int;
using pointer = value_type*;
using reference = value_type&;
// STL風(fēng)格的類型別名
using iterator = std::vector<value_type>::iterator;
using const_iterator = std::vector<value_type>::const_iterator;
private:
std::vector<value_type> data;
};
注意事項(xiàng)
在c++11及以后的c++版本中推薦使用using,不推薦使用typedef.
到此這篇關(guān)于c++之類型別名的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)c++ 類型別名內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
用VC++6.0實(shí)現(xiàn)石頭剪刀布游戲的程序
最先看到這個(gè)游戲代碼是python版的,后來看到有小伙伴用VC++重寫了一遍,運(yùn)行之后發(fā)現(xiàn)有些小bug,便嘗試這修復(fù)了一下,并增加了些小功能,這里分享給大家。2015-03-03
C語言實(shí)現(xiàn)簡單酒店管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C語言實(shí)現(xiàn)簡單酒店管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
VisualStudio2022制作多項(xiàng)目模板及Vsix插件的實(shí)現(xiàn)
本文主要介紹了VisualStudio2022制作多項(xiàng)目模板及Vsix插件的實(shí)現(xiàn),文中通過圖文介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-06-06
概率的問題:使用遞歸與多次試驗(yàn)?zāi)M的分析
以下對概率的問題:使用了遞歸和多次試驗(yàn)?zāi)M。需要的朋友參考下2013-05-05
C或C++報(bào)錯(cuò):ld returned 1 exit status報(bào)錯(cuò)的原因及解
這篇文章主要介紹了C或C++報(bào)錯(cuò):ld returned 1 exit status報(bào)錯(cuò)的原因及解決方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-02-02
C++11顯示類型轉(zhuǎn)換的優(yōu)點(diǎn)
這篇文章主要介紹了C++11顯示類型轉(zhuǎn)換的優(yōu)點(diǎn),幫助大家更好的理解和學(xué)習(xí)c++,感興趣的朋友可以了解下2020-08-08

