How do I overload functions in C?

Discussion in 'C Programming' started by some one, Oct 13, 2003.

  1. some one

    some one Guest

    I know that C++ lets you overload functions, but how do I overload functions in C?
    some one, Oct 13, 2003
    #1
    1. Advertising

  2. some one <> scribbled the following:
    > I know that C++ lets you overload functions, but how do I overload functions in C?


    You don't.

    --
    /-- Joona Palaste () ------------- Finland --------\
    \-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
    Joona I Palaste, Oct 13, 2003
    #2
    1. Advertising

  3. some one wrote:

    > I know that C++ lets you overload functions, but how do I overload functions in C?


    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
    Martin Ambuhl, Oct 13, 2003
    #3
  4. In article <>, some one wrote:
    > I know that C++ lets you overload functions, but how do I
    > overload functions in C?



    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
    Andreas Kahari, Oct 13, 2003
    #4
  5. some one

    lallous Guest

    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" <> wrote in message
    news:...
    > I know that C++ lets you overload functions, but how do I overload

    functions in C?
    lallous, Oct 13, 2003
    #5
  6. some one

    Fronsac Guest

    > > I know that C++ lets you overload functions, but how do I overload
    functions in C?
    > You don't.

    More precisely, you CAN'T.
    Fronsac, Oct 13, 2003
    #6
  7. some one wrote:

    > I know that C++ lets you overload functions, but how do I overload functions in C?


    Manually.

    > cat complex.c

    #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.
    E. Robert Tisdale, Oct 13, 2003
    #7
  8. some one

    John Bode Guest

    (some one) wrote in message news:<>...
    > I know that C++ lets you overload functions, but how do I overload functions in C?


    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;
    }
    John Bode, Oct 13, 2003
    #8
  9. some one

    jacob navia Guest

    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" <> wrote in message
    news:...
    > I know that C++ lets you overload functions, but how do I overload functions in C?
    jacob navia, Oct 13, 2003
    #9
  10. 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
    Arthur J. O'Dwyer, Oct 13, 2003
    #10
  11. some one

    James Hu Guest

    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
    James Hu, Oct 14, 2003
    #11
  12. jacob navia wrote:
    > With lcc-win32 you write:

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



    --
    Martin Ambuhl
    Martin Ambuhl, Oct 14, 2003
    #12
  13. some one

    jacob navia Guest

    "Arthur J. O'Dwyer" <> wrote in message
    news:p...
    >
    > 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?
    jacob navia, Oct 14, 2003
    #13
  14. "jacob navia" <> wrote in message news:<bmgs55$frt$>...
    > "Arthur J. O'Dwyer" <> wrote in message
    > news:p...
    > >
    > > 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.
    August Derleth, Oct 14, 2003
    #14
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eph0nk

    Error: Overload resolution failed

    Eph0nk, Oct 23, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    1,860
    Eph0nk
    Oct 23, 2003
  2. Piotre Ugrumov
    Replies:
    3
    Views:
    371
    Nick Hounsome
    Jan 25, 2004
  3. Xiangliang Meng
    Replies:
    1
    Views:
    1,595
    Victor Bazarov
    Jun 21, 2004
  4. Jared Ahern
    Replies:
    2
    Views:
    525
    Jared Ahern
    Sep 16, 2010
  5. Ying-Chieh Liao

    function overload (not operator overload)

    Ying-Chieh Liao, Oct 11, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    251
    Sherm Pendley
    Oct 11, 2004
Loading...

Share This Page