| |
I know that C++ lets you overload functions, but how do I overload functions in C? | | | | re: How do I overload functions in C?
some one <friedcheck@yahoo.com> scribbled the following:[color=blue]
> I know that C++ lets you overload functions, but how do I overload functions in C?[/color]
You don't.
--
/-- Joona Palaste (palaste@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/ | | | | re: How do I overload functions in C?
some one wrote:
[color=blue]
> I know that C++ lets you overload functions, but how do I overload functions in C?[/color]
You don't.
Whether overloading functions is a good thing is not decidable. The C++
designers have a different view from that of C designers.
--
Martin Ambuhl | | | | re: How do I overload functions in C?
In article <54a879f.0310130631.34339a5d@posting.google.com> , some one wrote:[color=blue]
> I know that C++ lets you overload functions, but how do I
> overload functions in C?[/color]
You can't.
There is one type of overloaded functions in C, the generic math
functions, but there's no way to overload functions yourself as
you may do in e.g. C++ or other languages.
--
Andreas Kähäri | | | | re: How do I overload functions in C?
You can't.
But perhaps you want to check the "..." that allows you to set
unlimited/variable argument list.
--
Elias http://lgwm.org/
"some one" <friedcheck@yahoo.com> wrote in message
news:54a879f.0310130631.34339a5d@posting.google.co m...[color=blue]
> I know that C++ lets you overload functions, but how do I overload[/color]
functions in C? | | | | re: How do I overload functions in C?
> > I know that C++ lets you overload functions, but how do I overload
functions in C?[color=blue]
> You don't.[/color]
More precisely, you CAN'T. | | | | re: How do I overload functions in C? friedcheck@yahoo.com (some one) wrote in message news:<54a879f.0310130631.34339a5d@posting.google.c om>...[color=blue]
> I know that C++ lets you overload functions, but how do I overload functions in C?[/color]
You can't, at least not in the C++ sense. You can fake it, sort of,
by using variable-length argument lists, but this isn't truly
overloading a function name.
Here's one approach. We create three functions to deal with different
type arguments (one handles double, one int, one char*). They're
called from one function with a variable-length argument list. The
first argument is mandatory, and it tells us what type of argument to
expect.
The va_start function sets ap to point to the first argument following
our fixed argument. The va_arg function returns the value of the
argument pointed to by ap, based on the type provided, and advances
the ap pointer to the next argument in the list.
#include <stdarg.h>
#include <stdio.h>
void func_1 (double f) { printf ("f = %f\n", f); }
void func_2 (int i) { printf ("i = %d\n", i); }
void func_3 (char *s) { printf ("s = %s\n", s); }
void func (int usage, ...)
{
va_list ap;
va_start (ap, usage);
switch (usage)
{
case 1: func_1 (va_arg (ap, double)); break;
case 2: func_2 (va_arg (ap, int)); break;
case 3: func_3 (va_arg (ap, char *)); break;
default: printf ("huh?\n"); break;
}
}
int main (void)
{
func (1, 1.0);
func (2, 2);
func (3, "three");
func (4, 40);
return 0;
} | | | | re: How do I overload functions in C?
some one wrote:
[color=blue]
> I know that C++ lets you overload functions, but how do I overload functions in C?[/color]
Manually.
[color=blue]
> cat complex.c[/color]
#include<complex.h>
#include<stdio.h>
int main(int argc, char* argv[]) {
double complex z = I;
double x = cabs(z);
fprintf(stdout, "%f = cabs(z)\n", x);
return 0;
}
You must manually *mangle* the function name --
usually by *decorating* it with prefixes and/or suffixes --
to create a unique *symbol* for each function argument type
that the link editor can use to find the appropriate function.
In the above example, 'c' is prepended to the abs function name
to calculate the absolute value of a complex number.
C++ compilers simply mangle the function name automatically
for C++ programmers depending upon the argument types.
In general, different C++ compilers use different *mangling schemes*
so it usually difficult to link object files
compiled by different C++ compilers. | | | | re: 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
"some one" <friedcheck@yahoo.com> wrote in message
news:54a879f.0310130631.34339a5d@posting.google.co m...[color=blue]
> I know that C++ lets you overload functions, but how do I overload functions in C?[/color] | | | | re: How do I overload functions in C?
On Mon, 13 Oct 2003, jacob navia wrote:[color=blue]
>
> "some one" <friedcheck@yahoo.com> wrote [...][color=green]
> > I know that C++ lets you overload functions, but how do
> > I overload functions in C?[/color]
>
> 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[/color]
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 | | | | re: How do I overload functions in C?
On 2003-10-13, some one <friedcheck@yahoo.com> wrote:[color=blue]
> I know that C++ lets you overload functions, but how do I overload
> functions in C?[/color]
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 | | | | re: How do I overload functions in C?
jacob navia wrote:[color=blue]
> With lcc-win32 you write:[/color]
[stuff that isn't C and isn't topical in comp.lang.c]
--
Martin Ambuhl | | | | re: How do I overload functions in C?
"Arthur J. O'Dwyer" <ajo@nospam.andrew.cmu.edu> wrote in message
news:Pine.LNX.4.58-035.0310131732340.24366@unix46.andrew.cmu.edu...[color=blue]
>
> On Mon, 13 Oct 2003, jacob navia wrote:[color=green]
> >
> > "some one" <friedcheck@yahoo.com> wrote [...][color=darkred]
> > > I know that C++ lets you overload functions, but how do
> > > I overload functions in C?[/color]
> >
> > 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[/color]
>
> 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
>[/color]
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? | | | | re: How do I overload functions in C?
"jacob navia" <jacob.navia@jacob.remcomp.fr> wrote in message news:<bmgs55$frt$1@news-reader4.wanadoo.fr>...[color=blue]
> "Arthur J. O'Dwyer" <ajo@nospam.andrew.cmu.edu> wrote in message
> news:Pine.LNX.4.58-035.0310131732340.24366@unix46.andrew.cmu.edu...[color=green]
> >
> > 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
> >[/color]
> OK OK True, this is an lcc-win32 evil extension...[/color]
Not evil, just off-topic. It isn't polite to post things outside of
the topic in a newsgroup, especially a technical one.
[color=blue]
> If this question arises it is because people need that isn't it?[/color]
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. |  | | | |