“通常,typedef并不引进新的类型”是什么意思?引进新的类型是什么意思?什么时候会引进新的类型?

申祷无 2013-07-31 11:40:55
RT。
...全文
248 11 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
signforlin 2013-08-23
  • 打赏
  • 举报
回复
通常。。。。
赵4老师 2013-08-07
  • 打赏
  • 举报
回复
typedef typedef type-declaration synonym(n.同义字); The typedef keyword defines a synonym for the specified type-declaration. The identifier in the type-declaration becomes another name for the type, instead of naming an instance of the type. You cannot use the typedef specifier inside a function definition. A typedef declaration introduces a name that, within its scope, becomes a synonym for the type given by the decl-specifiers portion of the declaration. In contrast to the class, struct, union, and enum declarations, typedef declarations do not introduce new types — they introduce new names for existing types. Example // Example of the typedef keyword typedef unsigned long ulong; ulong ul; // Equivalent to "unsigned long ul;" typedef struct mystructtag { int i; float f; char c; } mystruct; mystruct ms; // Equivalent to "struct mystructtag ms;" typedef int (*funcptr)(); // funcptr is synonym for "pointer // to function returning int" funcptr table[10]; // Equivalent to "int (*table[10])();"
lm_whales 2013-08-07
  • 打赏
  • 举报
回复
补充一下 C,C++ 还有以下,无符号数整型的原生类型, unsigned char,unsigned short int,unsigned int,unsigned long int,unsigned long long int; 以及不是类型的类型 void 指针不是新类型,是已定义类型。
lm_whales 2013-08-07
  • 打赏
  • 举报
回复
C,C++ 通用原生类型 char,short int,int,long int,long long int;float,double,long double; C++ 专用,原生类型 bool C,C++扩展类型: 1)C,C++通用,用户定义类型 enum union struct 2)C++专用,用户定义类型 class 3)C++ 模板类型 4)数组类型 ---集合类型 5)指针类型 6)函数,函数指针 4),5)是已有原生类型的扩展,不算定义新类型 6)定义函数,可能会定义新类型,不过个人认为不算,这个要看标准了。 1,2,3 用户定义类型,这个应该算,定义新类型。 typedef 有两种形式 1) 用已有类型 定义个新类型名字。 typedef int count; 2)定义新类型,同时定义一个名字 typedef struct tagCount{ int count; int shared; }Count; <===> struct tagCount{ int count; int shared; };//定义新类型,这里没有typedef 什么事,是 struct 定义了新类型struct tagCount。 typedef struct tagCount Count;//typedef为新类型struct tagCount 定义另一新名字Count。 typedef struct refCount{ int count; int shared; }refCount; <===> struct refCount{ int count; int shared; };//定义新类型,这里没有typedef 什么事,是 struct 定义了新类型struct refCount。 typedef struct refCount refCount;//typedef为新类型struct tagCount //定义另一新名字refCount。 定义的新类型和typedef无关, 只和 enum union struct class 这些关键字有关,这些关键字定义了新类型,typedef 只是为这些定义的新类型,定义另一个新名字。 总之typedef只是为现有类型,起个别名而已。
ForestDB 2013-08-01
  • 打赏
  • 举报
回复
忽略那个“通常”吧 typedef struct foo { } FOO; 这种也是先有新类型struct foo,然后给它个别名FOO。
lm_whales 2013-08-01
  • 打赏
  • 举报
回复
引用 5 楼 zhao4zhong1 的回复:
typedef只是给已有类型起了一个别名而已罢了。
++ 通常用短名字,替换长名字,或者简单写法替换复杂的写法。 typedef 没有定义一个,原本没有的类型,即没有引入新类型。
zhctj159 2013-07-31
  • 打赏
  • 举报
回复
静待高手、、
申祷无 2013-07-31
  • 打赏
  • 举报
回复
引用 1 楼 zhctj159 的回复:
struct会引进新的类型,class会引进新的类型,enum也可以引进新的类型、
引用 2 楼 max_min_ 的回复:

typedef 只是替代而已! 不会引进新的类型的!
如:
typedef int UNT    // 这句话 UNI 就是int的一个代替,并没有新的类型产生
定义结构的时候就是一个新的类型产生
如:
 struct student
{
   ....
};
student 就是一个新的类型!

我没说明白,我的意思是对于“通常,typedef并不引进新的类型”什么时候会引进新的类型,或者说typedef什么时候会引进新的类型。
max_min_ 2013-07-31
  • 打赏
  • 举报
回复

typedef 只是替代而已! 不会引进新的类型的!
如:
typedef int UNT    // 这句话 UNI 就是int的一个代替,并没有新的类型产生
定义结构的时候就是一个新的类型产生
如:
 struct student
{
   ....
};
student 就是一个新的类型!

zhctj159 2013-07-31
  • 打赏
  • 举报
回复
struct会引进新的类型,class会引进新的类型,enum也可以引进新的类型、
赵4老师 2013-07-31
  • 打赏
  • 举报
回复
typedef只是给已有类型起了一个别名而已罢了。

65,185

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧