Re: "data hiding" prototype code




"sofeng" <sofengboe@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


.



Relevant Pages

  • Re: Trying to make a struct external
    ... >> So, Rolf is right, put the struct definition into a header and include it ... First of all, you misspelled the header, the first letter of the file ... You can start by explaining why "globals are best avoided". ...
    (comp.lang.cpp)
  • Re: "data hiding" prototype code
    ... struct definition in the header. ... typedef struct foo_st foo_t; ...
    (comp.lang.c)
  • How to insert a ratEAPTLV attibute?
    ... Someone knows how to insert a ratEAPTLV (URI EAP-TLV)? ... I'm using one struct definition as: ... typedef struct _PEAPTLV_URI ...
    (microsoft.public.internet.radius)