@@ -7,35 +7,35 @@ actually contains. The reason to do that is not to allow a programmer to depend
7
7
anytime.
8
8
9
9
``` C
10
- /* Forward declaration in foo .h */
11
- struct foo ;
10
+ /* Forward declaration in opaque .h */
11
+ struct opaque ;
12
12
13
13
void
14
- do_stuff (struct foo * f);
14
+ do_stuff (struct opaque * f);
15
15
```
16
16
17
17
The file implementing `do_stuff()` **will cast the opaque structure object to
18
18
an internal structure type**.
19
19
20
20
```C
21
- /* in foo_impl .h not provided to the consumers */
22
- struct foo_impl {
21
+ /* in opaque_impl .h not provided to the consumers */
22
+ struct opaque_impl {
23
23
int x;
24
24
int y;
25
25
};
26
26
27
27
/* library implementation code */
28
28
void
29
- do_stuff(struct foo *f)
29
+ do_stuff(struct opaque *f)
30
30
{
31
- struct foo_impl *fi = (struct foo_impl *)f;
31
+ struct opaque_impl *fi = (struct opaque_impl *)f;
32
32
33
33
fi->x = ...
34
34
fi->y = ...
35
35
}
36
36
```
37
37
38
- Then any ` .c ` file that includes ` foo .h` can work with the structure
38
+ Then any ` .c ` file that includes ` opaque .h` can work with the structure
39
39
(by passing it to ` do_stuff() ` ) however cannot access its members directly.
40
40
The structure is usable only via a pointer. Thus, the library has to provide
41
41
a function to allocate a structure and return the pointer to it
@@ -45,20 +45,20 @@ inside the structure.
45
45
This is handy for libraries so they are free to change the layout of structures
46
46
without breaking the consumers.
47
47
48
- The consumer of the library then ` #include ` s only ` foo .h` but it does not have
49
- access to ` foo_impl .h` .
48
+ The consumer of the library then ` #include ` s only ` opaque .h` but it does not have
49
+ access to ` opaque_impl .h` .
50
50
51
51
``` C
52
52
#include < stdio.h>
53
53
54
- #include " foo .h"
54
+ #include " opaque .h"
55
55
56
56
int
57
57
main (void)
58
58
{
59
- struct foo * h;
59
+ struct opaque * h;
60
60
61
- h = getFoo ();
61
+ h = getOpaque ();
62
62
doStuff(h);
63
63
}
64
64
```
0 commit comments