Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > How do I overload functions in C?

Reply
Thread Tools

How do I overload functions in C?

 
 
James Hu
Guest
Posts: n/a
 
      10-14-2003
On 2003-10-13, some one <> wrote:
> I know that C++ lets you overload functions, but how do I overload
> functions in C?


You cannot do so arbitrarily, all C solutions require some form of
trickery. Several have been offered to you.

One trickery not yet suggested is to employ a polymorphic interface.

typedef struct polymorph polymorph;
struct polymorph {
void (* const foo)(polymorph *);
};

void foo(polymorph *p) { p->foo(p); }


Now, you get a form of function overloading when you create instances
of polymorph.

struct A {
polymorph interface;
int a;
};

static void A_foo(polymorph *p) {
struct A *me = (void *)p;
printf("a:%d\n", a++);
}

static const polymorph A_interface = { A_foo };

polymorph * create_A(void) {
struct A *i = malloc(sizeof(struct A));
i->interface = A_interface;
i->a = 0;
}

struct B {
polymorph interface;
int b;
};

static void B_foo(polymorph *p) {
struct B *me = (void *)p;
printf("b:%d\n", b--);
}

static const polymorph B_interface = { B_foo };

polymorph * create_B(void) {
struct B *i = malloc(sizeof(struct B));
i->interface = B_interface;
i->b = 0;
}

Now, you can get a form of overloading with the foo() function.

polymorph *a = create_A(); /* a points to a struct A */
polymorph *b = create_B(); /* b points to a struct B */

foo(a); /* will output "a:0" */
foo(b); /* will output "b:0" */
foo(a); /* will output "a:1" */
foo(b); /* will output "b:-1" */

-- James
 
Reply With Quote
 
 
 
 
Martin Ambuhl
Guest
Posts: n/a
 
      10-14-2003
jacob navia wrote:
> With lcc-win32 you write:

[stuff that isn't C and isn't topical in comp.lang.c]



--
Martin Ambuhl

 
Reply With Quote
 
 
 
 
jacob navia
Guest
Posts: n/a
 
      10-14-2003

"Arthur J. O'Dwyer" <> wrote in message
newsine.LNX.4.58-...
>
> On Mon, 13 Oct 2003, jacob navia wrote:
> >
> > "some one" <> wrote [...]
> > > I know that C++ lets you overload functions, but how do
> > > I overload functions in C?

> >
> > With lcc-win32 you write:
> > int overloaded fn(struct a*arg);
> > int overloaded fn(struct b *arg);
> >
> > and when you pass to fn an "a" it will call the first, and when you
> > pass it the second it will call the second.
> >
> > http://www.cs.virginia.edu/~lcc-win32

>
> That's very interesting, Jacob, but the OP did ask about
> the C language, not about lcc-win32.
>
> Oh, and *please* don't top-post.
>
> -Arthur
>

OK OK True, this is an lcc-win32 evil extension...
Sorry, I couldn't resist
If this question arises it is because people need that isn't it?


 
Reply With Quote
 
August Derleth
Guest
Posts: n/a
 
      10-14-2003
"jacob navia" <> wrote in message news:<bmgs55$frt$>...
> "Arthur J. O'Dwyer" <> wrote in message
> newsine.LNX.4.58-...
> >
> > That's very interesting, Jacob, but the OP did ask about
> > the C language, not about lcc-win32.
> >
> > Oh, and *please* don't top-post.
> >
> > -Arthur
> >

> OK OK True, this is an lcc-win32 evil extension...


Not evil, just off-topic. It isn't polite to post things outside of
the topic in a newsgroup, especially a technical one.

> If this question arises it is because people need that isn't it?


I've never needed it, and if I did I'd probably change languages.
(Although the idea of a variadic with the first argument declaring
type is neat...) But needs aren't really here or there when it comes
to topicality on this newsgroup. The topic is Standard C and/or
pre-Standard K&R C, and neither languages has any concept of function
overloading or polymorphism or inheritance or anything else of the
sort.

And, good job with curing yourself of top-posting! It's appreciated,
and makes people more likely to regard you as clueful.
 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Eliminate overload wrappers for extern "C" functions? Jared Ahern C++ 2 09-16-2010 10:08 PM
function overload (not operator overload) Ying-Chieh Liao Perl Misc 3 10-11-2004 11:24 AM
please help me in distinguish redefining functions, overloading functions and overriding functions. Xiangliang Meng C++ 1 06-21-2004 03:11 AM
How use the overload of>> (or<<) of a class in the overload of << and >> of another class? Piotre Ugrumov C++ 3 01-25-2004 08:08 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57