Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Does C support function overloading?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 866
    Comment on it

    Does C support function overloading?

    Function overloading is a feature available in most Object Oriented Languages such as C++ and Java. But C (not Object Oriented Language) doesnt support function overloading. Function overloading can be defined as defining many functions with same name but with different signatures. This feature can be implemented in C but indirectly. To achieve this feature, have a void * type of pointer as an argument to the function and another argument telling the actual data type of the first argument that is being passed.

    Function that can be defined in C:

     int demo(void * arg1, int arg2);
    

    Assume, arg2 as follows. 0 = Struct1 type variable, 1 = Struct2 type variable etc. and Struct1 and Struct2 are user defined struct types.

    Above function can be called at different places as:

       demo(arg1, 0);   /*Here, arg1 is pointer to struct type Struct1 variable*/
       demo(arg1, 1);    /*Here, arg1 is pointer to struct type Struct2 variable*/
    

    Now contents of demo function is:

    if(arg2 == 0)
    {
      struct1PtrVar = (Struct1 *)arg1;
    }
    else if(arg2 == 1)
    {
      struct2PtrVar = (Struct2 *)arg1;
    }
    else
    {
      /*Error Handling*/
    }
    

    Since the second argument of the demo keeps track the data type of the first type, inside the function demo, one can get the actual data type of the first argument by typecasting it accordingly. i.e. inside the demo function.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: