Friday, February 24, 2012

Function Overloading in C++!!

Function overloading: As most of you knew, it is another feature in C++. I will explain here about how compiler will know the function overloading and how it works. Function overloading is a concepts writing more than one function with a same name and different parameters. Function return type will not be consider for the function overloading.

          whenever calling the function with parameters, at the time of compilation, compiler will look for the exact match for the function by checking one by one. There are three possibilities. It wont maintain any dynamic table for all the functions.

Match found:  Finding exact match function.
void display(int a); // int as a argument

display(10);  // passing int as argument so exact match
No match found: if no match found, use type conversion and check for the match
void display(int a); //int as a argument


display('a'); // passing char as a argument, it wont find exact match, so type conversion will happen
Ambiguous match: exact match function found , but multiple times with different return type.
void display(int a); // return type as void and int as a argument 
int display(int a);  // Illegal , return type is differnt for the same function name and arguments 
  
display('a'); 

Type conversion:
  • char will convert to int
  • float will convert to double
  • enum will convert to int

No comments:

Popular Posts