为什么使用c ++ Typedef? [英] why use c++ Typedef?

查看:127
本文介绍了为什么使用c ++ Typedef?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对typedef的理解是给一个声明一个别名。
这样的int声明现在称为Integer。但为什么?为什么有人使用typedef?什么是更有利的原因?

My understanding of typedef is to give a declaration an alias. Such the declaration for int will now be referred to as Integer. But why? Why would someone use typedef? What's the more advantageous reason?

typedef int Integer;

Integer blamo = 50;

cout << blamo << endl;


推荐答案

这不是一个好的例子,因为 Integer int 没有任何不同的含义。但想象一下,

that isn't a good example because Integer and int don't have any different meaning. But imagine something like,

typedef int BlamoType;

现在,将来当'blamo'变成64位整数时, typedef和所有其他代码保持不变。

Now, in the future when 'blamo' becomes a 64-bit integer, you can just change this typedef, and all other code stays the same.

typedef的另一个原因是缩短类型名称。例如,而不是

Another reason for typedef is to shorten type names. For example instead of

boost::shared_ptr<std::map<std::wstring, std::vector<int> > > blamo1;
boost::shared_ptr<std::map<std::wstring, std::vector<int> > > blamo2;
std::vector<boost::shared_ptr<std::map<std::wstring, std::vector<int> > > > blamoList;

您可以:

typedef boost::shared_ptr<std::map<std::wstring, std::vector<int> > > BlamoType;
BlamoType blamo1;
BlamoType blamo2;
std::vector<BlamoType> blamoList;

这篇关于为什么使用c ++ Typedef?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆