Thread: function overloading in C

  1. #16
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    >>overloading is when you have 2 different versions of the function that take different arguments

    is this true. can't overloading just be redirecting the call to a user defined funtion rather than an ansi (or other standard) defined implementation?

    >>then all they did was provide theiur own definitions of the function

    is this not orbitz' definition of overloading?

    >>If it has a different function name, then you aren't overloading it.

    the funtion names are the same.

    if you include both <stdlib.h> and "dmalloc.h" (as long as its after the other includes) the code compiles. this is why dmalloc is such a powerful memory checker.

    i agree, its not overloading in the object-orientated sense. but functions are being redirected to the dmaloc library rather than the stdlib library.

  2. #17
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Function overloading:

    void myfun( int );
    void myfun( float );
    void myfun( char * );
    void myfun( struct myObject * );

    That's function overloading.
    Code:
    void somefun( something_here )
    {
        anotherfun( something_here ):
    }
    
    /*or*/
    #define somefunction somefun
    somefuction( x );
    Neither of those are overloading. One is basicly creating a ... what's the term I'm looking for ... an interface to use another function, and the second is just defining some other word to call a single function.

    "Overloading" is when you have more than one function with the same exact name that takes varried arguments combine with, optionally, different return types.

    C does not, as stated numerous times, support overloading.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #18
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    Yes, they have to have different arguments or else it's not overloading.

    They are probably using a macro or something to avoid having errors with redefinitions. Check out the header or point me to where I can download it and I'll be able to tell you exactly what it's doing.

  4. #19
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    ok, its not overloading.

    i was just making the point that you can define funtions in c that are defined elsewhere in included libraries using the same name.

    please bear in mind that i have been in the middle of a polystyrene fight throughout this dialog.

  5. #20
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    Originally posted by rotis23
    >>With function overloading you still have to write all the functions though, you just have them all the same name.

    by this definition, dmalloc overloads the malloc and free functions in c
    no, it replaces it; once you have #include'd dmalloc you cannot call the original gangster malloc()
    hello, internet!

  6. #21
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by rotis23
    ok, its not overloading.

    i was just making the point that you can define funtions in c that are defined elsewhere in included libraries using the same name.
    You can have more than one function with the same name in a program. They just can't be allowed to see eachother. Otherwise you'll get a compiler error. Example:

    File1
    Code:
    #include <stdio.h>
    static void myfun( void )
    {
        printf("File1:myfun( ) is called.\n");
    }
    void callfun1( void )
    {
        myfun( );
    }
    File2
    Code:
    #include <stdio.h>
    static void myfun( void )
    {
        printf("File2:myfun( ) is called.\n");
    }
    void callfun2( void )
    {
        myfun( );
    }
    File3
    Code:
    #include "myheader1.h"
    #include "myheader2.h"
    int main( void )
    {
        callfun1( );
        callfun2( );
        return 0;  
    }
    Compile each file seperately, in order. This assumes you have a header file for each of the others that basicly just prototypes the call function. Link and run.

    You can have different files in different modules with the same name. You just have to hide them from eachother so you don't have a problem.

    Originally posted by rotis23
    please bear in mind that i have been in the middle of a polystyrene fight throughout this dialog.
    Did you win?

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #22
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    I believe what rotis is talking about is interpositioning (??). It s talked about in Deep C Secrets by Van Dir Linden.

    It is how things like dmalloc and efence work.

  8. #23
    Registered User
    Join Date
    Aug 2002
    Posts
    351
    good debate guys!

    i think we've (maybe more me) sorted some things out.

    and no i didn't win....


    ...i was too busy debating with you lot!

  9. #24
    still a n00b Jaguar's Avatar
    Join Date
    Jun 2002
    Posts
    187
    C does not support function overloading.
    Variant arguments are not the same thing with function overloading. Have you ever seen something like...
    Code:
    #include <stdarg.h>
    
    float avg(char *start,...)
    {
    	int sum=0,n=0,arg;
    	va_list ap;
    	for (va_start(ap,start);arg=va_arg(ap,int);n++)
    		sum+=arg;
    	va_end(ap);
    	return (float)sum/n;
    }
    this function needs any numbers of arguments but we must expect type of the first argument to be char* and next arguments to be int (not float nor double, etc). You can not redefine this function to accept additional types. Hence it does not support function overloading.
    slackware 10.0; kernel 2.6.7
    gcc 3.4.0; glibc 2.3.2; vim editor
    migrating to freebsd 5.4

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Post your games here...
    By Hammer in forum Game Programming
    Replies: 132
    Last Post: 02-28-2013, 09:29 AM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Replies: 4
    Last Post: 11-23-2003, 07:15 AM