不透明的风格问题 [英] opaque style question

查看:57
本文介绍了不透明的风格问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨NG,


我已经读过这篇NG了,当你在写一个图书馆时

它可以是如果用户不需要自己修改结构,那么使用opaque类型指向你的
结构的好习惯。

这样lib的内部结构可以更改,并且可以重新编译lib

,但是使用lib的应用程序不需要重新编译。


但是有两种方法可以定义opque类型:

#typedef struct libstruct lib_t;



#typedef struct libstruct * lib_t;


我不确定使用哪一个。我还读到了某个地方,它很糟糕

练习将一个typedef设置为一个指针,使得它看起来并不是很明显。这个案例是lib_t,它是一个

指针。所以这意味着最好使用

第一个typedef。

但是如果我使用第一种方式,在应用程序中我需要声明

lib_t * my_opaque_variable;

如果我不这样做,我会收到编译错误,因为它

不知道它的大小lib_t。

这意味着第二个typedef会更好,因为

编译器总是知道指针的sizeof。


那么通常的做法是什么?


提前致谢,

Mark


-

<<删除电子邮件的del>>

Hi NG,

I''ve read some time ago in this NG, that when you''re writing a library
it can be good practice to use an opaque type to point to your
structures if the user doesn''t need to modify the structure himself.
This way the internal structure of the lib can be changed, and the lib
can be recompiled, but the application using the lib doesn''t need to be
recompiled.

But then there are two ways to define the opque type:
#typedef struct libstruct lib_t;
or
#typedef struct libstruct * lib_t;

I''m not sure which one to use. I also read somewhere that it''s bad
practice to make a typedef to a pointer in such a way that it is not
obvious from looking at the type, in this case lib_t, that it is a
pointer to whatever. So that would mean it would be best to use the
first typedef.
But if I use the first way, In the application I need to declare
lib_t *my_opaque_variable;
If I don''t do it that way, I will get a compiler error because it
doesn''t know the size of lib_t.
So that would mean the second typedef would be better, because the
compiler will always know the sizeof of a pointer.

So what is the usual way to do this?

Thanks in advance,
Mark

--
<<Remove the del for email>>

推荐答案

In< c5**********@news.tudelft.nl> Capstar< sp *** @ deleg.homeip.net>写道:
In <c5**********@news.tudelft.nl> Capstar <sp***@deleg.homeip.net> writes:
我已经读过这篇NG了,当你在写一个图书馆
时,最好使用opaque类型来如果用户不需要自己修改结构,请指向您的
结构。
这样可以更改lib的内部结构,并且可以重新编译lib
,但是使用lib的应用程序不需要重新编译。

然后有两种方法来定义opque类型:
#typedef struct libstruct lib_t;

#typedef struct libstruct * lib_t;

我不确定使用哪一个。我还在某处读到,对于指针进行类型设置是一种很糟糕的做法,以至于从查看类型(在本例中为lib_t)中它看起来并不明显,它是一个
指向任何东西。所以这意味着最好使用
第一个typedef。
但如果我使用第一种方式,在应用程序中我需要声明
lib_t * my_opaque_variable;
如果我不这样做,我会得到一个编译错误,因为它不知道lib_t的大小。
所以这意味着第二个typedef会更好,因为
编译器总是知道指针的sizeof。

那么通常的方法是什么?
I''ve read some time ago in this NG, that when you''re writing a library
it can be good practice to use an opaque type to point to your
structures if the user doesn''t need to modify the structure himself.
This way the internal structure of the lib can be changed, and the lib
can be recompiled, but the application using the lib doesn''t need to be
recompiled.

But then there are two ways to define the opque type:
#typedef struct libstruct lib_t;
or
#typedef struct libstruct * lib_t;

I''m not sure which one to use. I also read somewhere that it''s bad
practice to make a typedef to a pointer in such a way that it is not
obvious from looking at the type, in this case lib_t, that it is a
pointer to whatever. So that would mean it would be best to use the
first typedef.
But if I use the first way, In the application I need to declare
lib_t *my_opaque_variable;
If I don''t do it that way, I will get a compiler error because it
doesn''t know the size of lib_t.
So that would mean the second typedef would be better, because the
compiler will always know the sizeof of a pointer.

So what is the usual way to do this?




标准C库使用第一种方法。 FILE代表

真实的东西,你必须明确地声明它的指针。


Dan

-

Dan Pop

DESY Zeuthen,RZ集团

电子邮件: Da*****@ifh.de


Capstar< sp *** @ deleg.homeip.net>写道:

< snip>
Capstar <sp***@deleg.homeip.net> wrote:
<snip>
但是有两种方法来定义opque类型:
#typedef struct libstruct lib_t;

#typedef struct libstruct * lib_t;

我不确定使用哪一个。我还在某处读到,对于指针进行类型设置是一种很糟糕的做法,以至于从查看类型(在本例中为lib_t)中它看起来并不明显,它是一个
指向任何东西。所以这意味着最好使用
first typedef。


正确。

但是如果我使用第一种方式,在应用程序中我需要声明
lib_t * my_opaque_variable;


当然。有什么问题吗?

如果我不这样做,我会得到编译错误,因为它不知道lib_t的大小。


是的,显而易见的原因是它无法分辨

不完整类型的大小,直到实际完成为止。

这意味着第二个typedef会更好,因为
编译器总是知道指针的sizeof。


错误的结论。编译器_always_知道

指针的大小。编译器的*不知道的是不完整类型的

大小,但是根本没有改变

typedef''ing指向该类型的指针。记住:typedef确实

* NOT *引入一个新类型,只有一个已经存在的任意

类型的别名。

那么什么这是通常的方法吗?
But then there are two ways to define the opque type:
#typedef struct libstruct lib_t;
or
#typedef struct libstruct * lib_t;

I''m not sure which one to use. I also read somewhere that it''s bad
practice to make a typedef to a pointer in such a way that it is not
obvious from looking at the type, in this case lib_t, that it is a
pointer to whatever. So that would mean it would be best to use the
first typedef.
Right.
But if I use the first way, In the application I need to declare
lib_t *my_opaque_variable;
Of course. Any problems with that?
If I don''t do it that way, I will get a compiler error because it
doesn''t know the size of lib_t.
Yes, for the obvious reason that it cannot tell the size of an
incomplete type until it''s actually completed.
So that would mean the second typedef would be better, because the
compiler will always know the sizeof of a pointer.
Wrong conclusion. The compiler _always_ knows the size of a
pointer-to-whatever. What''s *not* known to the compiler is the
size of an incomplete type, but that''s not is changed at all by
typedef''ing a pointer to that type. Remember: typedef does
*NOT* introduce a new type, only an alias name for an arbitrary
type that already exists.
So what is the usual way to do this?




坚持使用第一个typedef(如果有任何typedef)并使用

指针表示法。正如你应该总是使用FILE *,而不是

任何模糊的typename别名。


HTH

问候

-

Irrwahn Grausewitz(ir*******@freenet.de)

欢迎来到clc: http://www.ungerhu.com/jxh/clc.welcome.txt

clc faq-list: http:/ /www.faqs.org/faqs/C-faq/faq/

clc OT指南: http://benpfaff.org/writings/clc/off-topic.html



Stick with the first typedef (if any typedef at all) and use the
pointer notation. Just as you should always use FILE *, and not
any obscure typename alias for it.

HTH
Regards
--
Irrwahn Grausewitz (ir*******@freenet.de)
welcome to clc: http://www.ungerhu.com/jxh/clc.welcome.txt
clc faq-list : http://www.faqs.org/faqs/C-faq/faq/
clc OT guide : http://benpfaff.org/writings/clc/off-topic.html


Capstar写道:
Capstar wrote:

我已经读过这篇NG了,当你在写一个
库时,使用不透明的做法是个好习惯如果用户不需要自己修改
结构,请键入
到您的结构。这样可以更改lib的内部结构,并且可以重新编译lib,但使用lib的
应用程序不需要重新编译。

但是有两种方法可以定义opque类型:
#typedef struct libstruct lib_t;

#typedef struct libstruct * lib_t;

我是不确定使用哪一个。我还在某处读到了这样一个不好的做法,就是以一种方式对一个指针进行一个typedef,这样看起来并不明显,在这种情况下,lib_t,
它是指向任何东西的指针。所以这意味着它最好使用第一个typedef。
但如果我使用第一种方式,在应用程序中我需要声明
lib_t * my_opaque_variable;
如果我不这样做,我会得到一个编译错误,因为它不知道lib_t的大小。
所以这意味着第二个typedef会更好,因为
那么通常的方法是什么?

I''ve read some time ago in this NG, that when you''re writing a
library it can be good practice to use an opaque type to point
to your structures if the user doesn''t need to modify the
structure himself. This way the internal structure of the lib
can be changed, and the lib can be recompiled, but the
application using the lib doesn''t need to be recompiled.

But then there are two ways to define the opque type:
#typedef struct libstruct lib_t;
or
#typedef struct libstruct * lib_t;

I''m not sure which one to use. I also read somewhere that it''s
bad practice to make a typedef to a pointer in such a way that
it is not obvious from looking at the type, in this case lib_t,
that it is a pointer to whatever. So that would mean it would
be best to use the first typedef.
But if I use the first way, In the application I need to declare
lib_t *my_opaque_variable;
If I don''t do it that way, I will get a compiler error because
it doesn''t know the size of lib_t.
So that would mean the second typedef would be better, because
the compiler will always know the sizeof of a pointer.

So what is the usual way to do this?




目标是隐藏结构。这意味着你永远不能在用户代码中声明一个struct libstruct,因为

的大小是不可知的。但是你可以声明一个指针类型,因为

大小是已知的。现在你不能为那个指针malloc任何内存,

,因为它的大小还不知道。这意味着库必须具有执行mallocing并返回指针的例程

。所有

用户代码都可以保存指针并将其返回到库中的各种

例程,这些例程知道如何处理它。如果指针的
mallocing涉及mallocing子字段,

处理也必须是库例程。


-

fix(vb。):1。纸张覆盖,模糊,隐藏在公众视野之外; 2.

以一种产生意想不到的后果的方式来解决,比原来的问题更糟糕。用法:Windows ME

修复了Windows 98 SE的许多缺点。 - 和记



The objective is to hide the structure. That means you can never
declare a struct libstruct in the user code, because the size
isn''t known. However you can declare a pointer type, because that
size is known. Now you can''t malloc any memory for that pointer,
because the size isn''t known. That means the library has to have
a routine to do the mallocing and return the pointer. All the
user code can do is save the pointer and return it to various
routines in the library, which know what to do with it. If the
mallocing for the pointer involves mallocing subfields, the
disposal has also to be a library routine.

--
fix (vb.): 1. to paper over, obscure, hide from public view; 2.
to work around, in a way that produces unintended consequences
that are worse than the original problem. Usage: "Windows ME
fixes many of the shortcomings of Windows 98 SE". - Hutchison


这篇关于不透明的风格问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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