Re: "data hiding" prototype code



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_*/

.



Relevant Pages

  • Re: [ANNOUNCE,RFC] rcuhashbash synchronization benchmark module
    ... Details of the algorithm appear later in this mail. ... Inserting the item into the new bucket before removing it from the old ... struct rcuhashbash_bucket { ... goto unlock_and_loop; ...
    (Linux-Kernel)
  • [ANNOUNCE,RFC] rcuhashbash synchronization benchmark module
    ... I developed a new algorithm for moving a hash table ... Inserting the item into the new bucket before removing it from the old ... struct rcuhashbash_bucket { ... goto unlock_and_loop; ...
    (Linux-Kernel)
  • [PATCH 1/2] sound: Use usb_set/get_intfdata
    ... @same depends on header@ ... struct usb_interface*intf; ... static void snd_disconnect ... void *chip; ...
    (Linux-Kernel)
  • Code vectorization syntax woes (C++ or other?)
    ... struct A { ... If this is used as a static part of some other algorithm, inlining will give optimal code, ... Often library authors will even move the vectorizing loop into the algorithm, ... When this chain is set up at runtime, every little part of glue is called by function pointer, so ...
    (comp.lang.misc)
  • Re: "data hiding" prototype code
    ... However, assuming that Algorithm really does own this data, I would ... Algorithmcan return a pointer to this ... I *did* define the struct inside algorithm.c (named ... void GetAlgorithmData ...
    (comp.lang.c)