B
Bill Pursell
I've been thinking of doing things like the following:
[tmp]$ cat foo.h
struct foo{
int a;
void (*init)(struct foo *, int);
};
void new_foo(struct foo *self);
[tmp]$ cat foo.c
#include "foo.h"
static void init(struct foo *self, int a)
{
self->a = a;
}
void new_foo(struct foo *self)
{
self->init=init;
}
[tmp]$ cat a.c
#include "foo.h"
#include <stdio.h>
int main()
{
struct foo f;
new_foo(&f);
f.init(&f,10);
/* can't call init from foo.c directly, because it was declared
static */
}
Basically, I'm providing what in other languages might be called
(pseudo)-private member function to foo. I don't understand the static
linkage mechanism well enough to know if this is a bad idea. It seems
to work well, but I've only used it in very small toy test cases. Are
there any reasons to avoid this type of construction?
[tmp]$ cat foo.h
struct foo{
int a;
void (*init)(struct foo *, int);
};
void new_foo(struct foo *self);
[tmp]$ cat foo.c
#include "foo.h"
static void init(struct foo *self, int a)
{
self->a = a;
}
void new_foo(struct foo *self)
{
self->init=init;
}
[tmp]$ cat a.c
#include "foo.h"
#include <stdio.h>
int main()
{
struct foo f;
new_foo(&f);
f.init(&f,10);
/* can't call init from foo.c directly, because it was declared
static */
}
Basically, I'm providing what in other languages might be called
(pseudo)-private member function to foo. I don't understand the static
linkage mechanism well enough to know if this is a bad idea. It seems
to work well, but I've only used it in very small toy test cases. Are
there any reasons to avoid this type of construction?