Re: "data hiding" prototype code
- From: "sofeng" <sofengboe@xxxxxxxxx>
- Date: 12 Apr 2007 09:41:53 -0700
On Apr 11, 4:39 pm, "Jonas" <spamhereple...@xxxxxxxxx> wrote:
"sofeng" <sofeng...@xxxxxxxxx> wrote in message
news:1176328415.501183.20910@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Thank you for your reply. See my comments below.
On Apr 11, 1:18 pm, "bluejack" <bluuj...@xxxxxxxxx> wrote:
First of all, making the variables static at file scope doesn't really
achieve the goal of encapsulation, particularly when you publish the
struct definition in the header. Secondly, static variables at file
scope are not much better than globals: they're certainly not thread
safe, and you open yourself to other sorts of errors as well.
I'm not sure what you mean by "publish the struct definition in the
header". I only put the typedef in the header-- that doesn't publish
the struct definition, right?
In your header file, you have
typedef struct {
double data1;
double data2;
double data3;
} ALGORITHM_DATA;
Instead, separate the typedef and the struct definition into header and
implementation files:
/* foo.h */
typedef struct foo_st foo_t;
/* foo.c */
#include "foo.h"
struct foo_st {
int bar;
};
Now, struct foo_st will be undefined outside of foo.c, which is what you
want to achieve. Pointers to foo_t can be defined by users of foo, but foo_t
objects cannot, leaving your code in charge of this and anything that has to
do with foo_t internals.
--
Jonas
But struct foo_st is synonymous with foo_t so why does it matter that
it is available outside of foo.c? Don't we really care about the
actual variable definition (where the memory is allocated) of type
"struct foo_st" or "foo_t"? For example, for the definition of
foo_var, "foo_t foo_var", foo_var should not be made available outside
of foo.c.
.
- Follow-Ups:
- Re: "data hiding" prototype code
- From: Jonas
- Re: "data hiding" prototype code
- References:
- "data hiding" prototype code
- From: sofeng
- Re: "data hiding" prototype code
- From: bluejack
- Re: "data hiding" prototype code
- From: sofeng
- Re: "data hiding" prototype code
- From: Jonas
- "data hiding" prototype code
- Prev by Date: Re: Segmentation Fault.... C99F
- Next by Date: Re: Segmentation Fault.... C99F
- Previous by thread: Re: "data hiding" prototype code
- Next by thread: Re: "data hiding" prototype code
- Index(es):
Relevant Pages
|