📄 opaque.examp.html
字号:
<html><!-- Mirrored from c-faq.com/struct/opaque.examp.html by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 07:59:27 GMT --><head><title></title></head><body>[This is extracted from a mail message I sent on 1997-09-23,to someone who asked for an example.]<p>From: scs@eskimo.com (Steve Summit)<br>Subject: Re: ADT??<br>Date: Tue, 23 Sep 1997 18:21:38 -0700 (PDT)<br>Message-Id: <199709240121.SAA03216@mail.eskimo.com><p>...you might have, in <TT>adt.h</TT>, the declarations:<p><pre> extern struct adt *newadt(void); extern int processadt(struct adt *); extern void freeadt(struct adt *);</pre><p>Then a client could do:<p><pre> #include "adt.h" struct adt *theadt = newadt(); processadt(theadt); freeadt(theadt);</pre><p>The actual implementation of the ADT would start out like this:<p><pre> #include "adt.h" struct adt { /* actual structure definition */ }; struct adt * newadt(void) { ...</pre><p>Or, if the ADT's implementation comprises several source files,you could put the structure definition in, say, <TT>adtinternals.h</TT>:<p><pre> struct adt { /* actual structure definition */ };</pre><p>Then, include both <TT>adt.h</TT> and <TT>adtinternals.h</TT> in each source fileimplementing the ADT:<p><pre> #include "adt.h" #include "adtinternals.h" struct adt * newadt(void) { ...</pre><p>Occasionally, I find it useful to centralize all declarationsconcerning an ADT into one header file (<TT>"adt.h"</TT>, in thecontinuing example), in which case I do it like this:<p><pre> #ifdef ADT_INTERNALS struct adt { /* actual structure definition */ }; #endif extern struct adt *newadt(void); extern int processadt(struct adt *); extern void freeadt(struct adt *);</pre><p>Then I define <TT>ADT_INTERNALS</TT> in each source file implementing theADT, just before including <TT>adt.h</TT>:<p><pre> #define ADT_INTERNALS #include "adt.h" struct adt * newadt(void) { ...</pre><p>This is obviously a weaker form of information hiding, becauseit's easier for a client to ``cheat'' and peek at the type'simplementation.<p>Finally, when you use the first form, with the incompletestructure type, it's often useful to place an incompletestructure declaration in the public header file:<p><pre> struct adt; extern struct adt *newadt(void); extern int processadt(struct adt *); extern void freeadt(struct adt *);</pre><p>This prevents certain compiler warnings, and averts a couple ofother obscure possible problems.</body><!-- Mirrored from c-faq.com/struct/opaque.examp.html by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 07:59:27 GMT --></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -