Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > simulating private member functions--static linkage

Reply
Thread Tools

simulating private member functions--static linkage

 
 
Bill Pursell
Guest
Posts: n/a
 
      04-27-2006
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?

 
Reply With Quote
 
 
 
 
jefong@56.com
Guest
Posts: n/a
 
      04-27-2006

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?


 
Reply With Quote
 
 
 
 
50h d'essai
Guest
Posts: n/a
 
      04-27-2006
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


 
Reply With Quote
 
jefong@56.com
Guest
Posts: n/a
 
      04-27-2006
you are only declare a function type pointer.so it need to be assigned,
then you can call it.

 
Reply With Quote
 
Michael Wojcik
Guest
Posts: n/a
 
      05-01-2006

In article <(E-Mail Removed) .com>, "Bill Pursell" <(E-Mail Removed)> 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 http://www.velocityreviews.com/forums/(E-Mail Removed)

The presence of those seeking the truth is infinitely preferable to
the presence of those who think they've found it. -- Terry Pratchett
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
private member function or non-member utility function ittium C++ 5 01-12-2012 08:56 AM
What are the naming convention for private member variable, andprivate and public member function? Peng Yu Python 3 09-21-2009 04:49 AM
c++ linkage vs c linkage ramasubramanian.rahul@gmail.com C++ 1 09-12-2008 11:41 AM
Simulating inheritance and non-member functions in C Doug Eleveld C Programming 3 10-24-2003 11:06 PM
linkage of class member functions qazmlp C++ 0 07-17-2003 05:05 AM



Advertisments