By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,368 Members | 1,318 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,368 IT Pros & Developers. It's quick & easy.

simulating private member functions--static linkage

P: n/a
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?

Apr 27 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a

Bill Pursell wrote:
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);
// I think you should declare the function's type in here.
void init(struct foo *self, int a) [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?


Apr 27 '06 #2

P: n/a
Hello Bill Pursell, All !
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;
}

<snip>

[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 */
}
can't call init from a.c maybe ?

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?


In C AFAIK function is considered as a global object. This avoids to
place keyword extern every time. static keyword limits the range of
visibility of that function to source file within it is declared. Hope
it helps.

--
50h d'essai
06300 Nice
e-mail: katsuo_harada_evil_does (at) hotmail (.) com
All said above is IMHO
Apr 27 '06 #3

P: n/a
you are only declare a function type pointer.so it need to be assigned,
then you can call it.

Apr 27 '06 #4

P: n/a

In article <11**********************@y43g2000cwc.googlegroups .com>, "Bill Pursell" <bi**********@gmail.com> writes:
I've been thinking of doing things like the following:

[snip code that assigns a static function's address to a function
pointer in a structure, then calls the function through that pointer]

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?


It's safe; static linkage limits the visibility of a function, but
you're allowed to call that function from a scope where it isn't
visible via a function pointer. This sort of construct is commonly
used to implement polymorphism in C (that is, it's commonly used in
programs where the author wants to implement polymorphism).

I think it's a useful technique. There are many possible variations;
for example, some people prefer to write factory functions that
allocate the structure for the caller. But the short answer is that
what you're doing is fine.

(Of course, there are always reasons to avoid any construction in
some circumstances; you wouldn't do this for every single function
in your program. Used judiciously it's quite useful.)

--
Michael Wojcik mi************@microfocus.com

The presence of those seeking the truth is infinitely preferable to
the presence of those who think they've found it. -- Terry Pratchett
May 1 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.