Re: "data hiding" prototype code
- From: "sofeng" <sofengboe@xxxxxxxxx>
- Date: 11 Apr 2007 17:15:16 -0700
On Apr 11, 1:18 pm, "bluejack" <bluuj...@xxxxxxxxx> wrote:
Your design does not make it clear who "owns" this data. An algorithmThank you for your reply.
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 named it "Object". My intention was that "algorithm.c" is the
owner of the data. I *did* define the struct inside algorithm.c (named
"alg") and provided the typedef in algorithm.h (named
"ALGORITHM_DATA"). "ALGORITHM_DATA" is a typedef, not a variable
definition. Maybe the name in all caps was confusing. Maybe I should
have named it "AlgorithmDataType".
I didn't want to return a pointer to the structure because in my
actual code, I want a function other than the caller of Algorithm() to
get access to algorithm.c's data directly. For example, I want
Algorithm2(), which does not call Algorithm() to be able to call
GetAlgorithmData() to get access to the data structure defined in
algorithm.c. I realize I did not specify this calling structure in my
oversimplified example. I have added the Algorithm2() code below.
Please let me know if I have misunderstood you.
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_*/
.
- References:
- "data hiding" prototype code
- From: sofeng
- Re: "data hiding" prototype code
- From: bluejack
- "data hiding" prototype code
- Prev by Date: Re: parsing text
- Next by Date: Re: "data hiding" prototype code
- Previous by thread: Re: "data hiding" prototype code
- Next by thread: Re: "data hiding" prototype code
- Index(es):
Relevant Pages
|