Re: "data hiding" prototype code
- From: "sofeng" <sofengboe@xxxxxxxxx>
- Date: 11 Apr 2007 14:53:35 -0700
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?
Your design does not make it clear who "owns" this data. An algorithm
is typically a particular pattern applied to someone else's data,
although it may store some of it's own in the process. Thus, using
"Algorithm" as the generic placeholder for your OO class could be
confusing. Typical algorithms will *take* data and return *results*.
However, assuming that Algorithm really does own this data, I would
define the struct inside the .c file, providing the typedef in the .h
file. *This* hides your data. Algorithm() can return a pointer to this
type, and then you can call accessors with the pointer to obtain
individual elements of the data.
I realize that the name "Algorithm" might be confusing. Maybe I should
have called it "Object". My intention is that "algorithm.c" "owns" the
data. I did define the struct inside the .c file (it is named "alg")
and I provided the typedef in the header file ( named
"ALGORITHM_DATA"). Maybe my naming was confusing-- maybe I should have
named it "AlgorithmDataType"? I do not want Algorithm() to directly
return the pointer to the data because its caller does not use the
data directly. I want to be able to use algorithm.c's data from
another function, say, Algorithm2(), which does not call Algorithm().
I want Algorithm2() to call algorithm.c's accessor function,
GetAlgorithmData() to get a pointer to a const ALGORITHM_DATA. I put
the const qualifier so that Algorithm2() would not be able to modify
the data in algorithm.c. I realize this calling structure was not
evident in my over-simplified example. I modified my example to
include Algorithm2(). Let me know if I have misunderstood you.
-sofeng
main.c:
#include "algorithm.h"
#include "algorithm2.h"
int main(void)
{
Algorithm();
Algorithm2();
return 0;
}
algorithm.c:
#include "algorithm.h"
static ALGORITHM_DATA alg;
void GetAlgorithmData(const ALGORITHM_DATA **out)
{
*out = &alg;
}
void Algorithm(void)
{
alg.data1 = 1.0;
alg.data2 = 2.0;
alg.data2 = 3.0;
}
algorithm.h:
#ifndef ALGORITHM_H_
#define ALGORITHM_H_
typedef struct {
double data1;
double data2;
double data3;
} ALGORITHM_DATA;
void GetAlgorithmData(const ALGORITHM_DATA **out);
void Algorithm(void);
#endif /*ALGORITHM_H_*/
algorithm2.c:
#include <stdio.h>
#include "algorithm.h"
void Algorithm2(void)
{
const ALGORITHM_DATA *alg_ptr;
GetAlgorithmData(&alg_ptr);
/*alg_ptr->data1 = 99.9;*/ /* This is not allowed */
/* do something with data here */
printf("%f\n", alg_ptr->data1);
printf("%f\n", alg_ptr->data2);
printf("%f\n", alg_ptr->data3);
}
algorithm2.h:
#ifndef ALGORITHM2_H_
#define ALGORITHM2_H_
void Algorithm2(void);
#endif /*ALGORITHM2_H_*/
.
- 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
- "data hiding" prototype code
- Prev by Date: Re: Facing Problem in Loop
- Next by Date: Re: Segmentation Fault....
- Previous by thread: Re: "data hiding" prototype code
- Next by thread: Re: "data hiding" prototype code
- Index(es):
Relevant Pages
|