使用typedef有什么好处吗? [英] is it any good to use typedef ?

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

问题描述



我总是有这样的想法,即typedef数据类型特别是结构

编码非常方便,但我的老师坚持认为我应该使用

完整的结构声明,没有进一步的解释,所以我想知道

有什么好用的typedef?而且我也知道当一个数据类型

被typedefed变成一个抽象数据类型时,究竟什么是一个抽象数据类型,它有什么用呢?

-

通过 http://dbforums.com

推荐答案

dreamcatcher< me ********* @ dbforums.com>潦草地写了下面的内容:
dreamcatcher <me*********@dbforums.com> scribbled the following:
我总是有这样的想法,即typedef数据类型特别是结构
在编码时非常方便,但我的老师坚持认为我应该使用
完整的结构声明,没有进一步的解释,所以我想知道
有什么好用的typedef?而且我也知道,当一个被类型化的数据类型成为抽象数据类型时,究竟什么是抽象数据类型,它有什么用呢?
I always have this idea that typedef a data type especially a structure
is very convenient in coding, but my teacher insisted that I should use
the full struct declaration and no further explanations, so I wonder is
there any good using typedef ? and I also know that when a data type
being typedefed become an abstract data type, so what exactly is an
abstract data type, is it any good ?



Typedeffing结构是一个坏主意,但有* *
typedef的良好用途。特别是函数指针。


假设你想要一个函数,它接受一个函数数组

指针(实际上是指向函数指针的指针)并返回一个

它得到的函数指针。这些函数每个都需要多个

参数并返回一个值。 (假设它们的形式为int

function(int,int)。)

你会如何为这种函数编写原型?没有

typedef,它*是*可能的,但我甚至都不会尝试它。一些C

专家将为您提供一个。

但是,使用typedef,它很容易:

typedef int(* func_ptr)( int,int);

func_ptr selectOneOfTheFunctions(func_ptr functions []);


-

/ - Joona Palaste( pa*****@cc.helsinki.fi)--------------------------- \

| 飞翔的柠檬树中的金鸡王G ++ FR FW + M-#108 D + ADA N +++ |

| http://www.helsinki.fi/~palaste W ++ B OP + |

\ -----------------------------------------芬兰的规则! ------------ /

你可以夺走他的生命......

- Mirja Tolsa



Typedeffing structs is a bad idea, but there *are* good uses for
typedef. Particularly with function pointers.

Suppose you want a function that takes in an array of function
pointers (actually a pointer to function pointers) and returns one of
the function pointers it got. These functions each take multiple
parameters and return one value. (Suppose they are of the form int
function(int, int).)
How would you write the prototype for this kind of function? Without
typedef, it *is* possible, but I''m not even going to try it. Some C
expert will provide you one.
However, with typedef, it''s easy:
typedef int (*func_ptr)(int, int);
func_ptr selectOneOfTheFunctions(func_ptr functions[]);

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"You could take his life and..."
- Mirja Tolsa


Joona I Palaste写道:
Joona I Palaste wrote:
dreamcatcher< me ********* @ dbforums.com>潦草地写了下面的内容:
dreamcatcher <me*********@dbforums.com> scribbled the following:
我总是有这样的想法,即typedef数据类型特别是一个结构
编码非常方便,但我的老师坚持认为我应该使用
完整的结构声明,没有进一步的解释,所以我想知道
有什么好用的typedef?而且我也知道当一个数据类型被typedefed变成一个抽象数据类型时,究竟什么是抽象数据类型,它有什么用呢?

Typedeffing结构是一个坏主意,
I always have this idea that typedef a data type especially a structure
is very convenient in coding, but my teacher insisted that I should use
the full struct declaration and no further explanations, so I wonder is
there any good using typedef ? and I also know that when a data type
being typedefed become an abstract data type, so what exactly is an
abstract data type, is it any good ?

Typedeffing structs is a bad idea,




恐怕这个话题非常神圣。无论如何,让我来b / b
不同意你的意见。对于typedef'

结构,有很好的用法。假设您正在创建一个抽象数据类型foo。你可以创建一个foo类型的对象,销毁foo类型的对象并禁止类型为foo的
对象。现在你选择将foo实现为结构,但

这不是你的接口设计所必需的。我知道的一个好方法

如何做到这一点就是提供以下界面:


/ * ---------- --------- 8< --- foo.h中------------------------------ * /

#ifndef FOO_H

#define FOO_H


typedef struct foo_tag foo;


foo * foo_create(void);

void foo_bar(foo * f);

void foo_bar(foo * f);


#endif / * FOO_H * /

/*-------------------8<---foo.h----- ------------------------ * /


/ * ---------- --------- 8< --- foo.c的----------------------------- * / <无线电通信/>
#include< stdlib.h>

#include" foo.h"


struct foo_tag

$

int baz;

};


foo * foo_create(无效)

{

foo * f;

f = malloc(sizeof * f);

if(NULL!= f)

{

f.baz = 0;

}

返回f;

}


void foo_bar(foo * f)

{

f.baz ++;

}


void foo_destroy(foo * f)

{

免费(f);

}

/ * ------------------- 8< --- foo.c ----------------------------- * /


那天你重构你的代码并发现foo要好得多

做一个unsigned long而不是一个结构,那么你所要做的就是

改变typedef line in你的标题并重写你的实现,

和,O gloria,allelluya,你的界面没有改变!


我有兴趣听到你的论据(如果有的话)为什么它不是一个

好​​主意在这种情况下输入结构...

但是*有*是 typedef。



I am afraid this subject is pretty holy-warish. At any rate, let me
disagree with you. There are occurences of good usage for a typedef''d
structure. Let''s say you are creating an abstract data type, foo. You
can create an objet of type foo, destroy an objet of type foo and bar an
objet of type foo. For now you choose to implement foo as a struct, but
this is not required by your interface design. The one good way I know
of how to do that is to offer the following interface:

/*-------------------8<---foo.h------------------------------*/
#ifndef FOO_H
#define FOO_H

typedef struct foo_tag foo;

foo *foo_create(void);
void foo_bar(foo *f);
void foo_bar(foo *f);

#endif /* FOO_H */
/*-------------------8<---foo.h-----------------------------*/

/*-------------------8<---foo.c-----------------------------*/
#include <stdlib.h>
#include "foo.h"

struct foo_tag
{
int baz;
};

foo *foo_create(void)
{
foo *f;
f = malloc(sizeof *f);
if (NULL != f)
{
f.baz = 0;
}
return f;
}

void foo_bar(foo *f)
{
f.baz++;
}

void foo_destroy(foo *f)
{
free(f);
}
/*-------------------8<---foo.c-----------------------------*/

The day you refactor your code and find out that foo is much better
doing as an unsigned long than a structure, then all you have to do is
change the typedef line in your header and rewrite your implementation,
and, O gloria, allelluya, your interface hasn''t changed !

I''d be interested to hear your arguments (if any) for why it is not a
good idea to typedef the struct in this case...
but there *are* good uses for
typedef.




确实如此;-)


-

Bertrand Mollinier Toublet

自行车就像女士一样,如果你不经常照顾它们,

当你想要回到它们身上时,需要花很多时间工作k"

- Riccardo Turchetto



Yes indeed ;-)

--
Bertrand Mollinier Toublet
"Bikes are like ladies, if you don''t take care of them all the time,
when you feel like going back to them, it takes a lot of work"
-- Riccardo Turchetto


Joona I Palaste写道:
Joona I Palaste wrote:

dreamcatcher< me ********* @ dbforums.com>潦草地写了下面的内容:

dreamcatcher <me*********@dbforums.com> scribbled the following:
我总是有这样的想法,即typedef数据类型特别是结构
在编码时非常方便,但我的老师坚持认为我应该使用
完整的结构声明,没有进一步的解释,所以我想知道
有什么好用的typedef?而且我也知道,当一个被类型化的数据类型成为抽象数据类型时,究竟什么是抽象数据类型,它有什么用呢?
I always have this idea that typedef a data type especially a structure
is very convenient in coding, but my teacher insisted that I should use
the full struct declaration and no further explanations, so I wonder is
there any good using typedef ? and I also know that when a data type
being typedefed become an abstract data type, so what exactly is an
abstract data type, is it any good ?


Typedeffing结构是一个坏主意,但有* *
typedef的良好用途。特别是功能指针。 [...]



Typedeffing structs is a bad idea, but there *are* good uses for
typedef. Particularly with function pointers. [...]




使用或避免使用struct和union类型的typedef

似乎是一个品味问题。我没有看到任何坚实的技术理由支持一种做法而不是另一种做法,所以

归结为de gustibus。 (就个人而言,我在

使用-typedef阵营 - 但我没有特别的争吵

与那些不同的人。)


除了Joona的使用typedef的例子

澄清粗糙的函数声明,我找到的另一个地方

typedef有用的是在容纳关于C'类型大小的不确定性。例如,如果我需要一个能够表示高达一百万的值的整数

类型,那么使用`int''它是不安全的因为它可能只会高as

32767.`long''至少会达到2147483647,因此

就足够了,但在某些机器上long将会是

严重的过度杀伤和浪费空间。如果我需要存储大量的这些数字,我想使用最小的

可能的类型。


预处理器测试与typedef相结合,提供了一种方法,可以轻松地处理这个
,并且具有最小的丑陋:


#include< limits.h>

#if CHAR_MIN< = -1000000&& 1000000< = CHAR_MAX

typedef char Million;

#elif SHRT_MIN< = -1000000&& 1000000< = SHRT_MAX

typedef short Million;

#elif INT_MIN< = -1000000&& 1000000< = INT_MAX

typedef int百万;

#else

typedef long百万; / *已知足够* /

#endif


此后,每当我的意思是'a
$时我就可以写'百万' b $ b足够但不是不必要的宽整数:


百万* ptr = malloc(1234567 * sizeof * ptr);

百万最小,最大;

百万func(无效);


....等等;所有蹩脚决策的结果

已被方便地打包成单词Million。


但是,一个缺点就是说明了此代码:


百万价值= 42;

printf(" value =%d \ n",value); / *错误* /


问题是,如果百万实际上证明是长的

别名,则 %d"格式说明符应该是%ld

。解决此问题的一种方法是将FMT_MILLION

宏定义为d。或ld或ld在上面的测试中;你可以

然后写


printf(" value =%" FMT_MILLION&qu​​ot; \ n",value);


这样可行,但写一些笨拙并使一些

种工具搞得一团糟,这些工具可以帮助将消息字符串翻译成多种语言。我更喜欢更简单的


printf(" value =%ld \ n",(long)value);


...即使它在运行时可能会花费更多。


简而言之:当你想隐藏时,typedef很方便

详细信息最终用户的某些类型决定

(可能是你自己)。


-
Er ********* @sun.com


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

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