Function overloading and return type

 

  In C++ and Java, functions can not be overloaded if they differ only in the return type.

  For example, the following program C++ and Java programs fail in compilation.

  (1)C++ Program

 1 #include<iostream>
 2 int foo() 
 3 { 
 4     return 10; 
 5 }
 6  
 7 char foo() {  // compiler error; new declaration of foo()
 8     return 'a'; 
 9 }
10  
11 int main()
12 {
13     char x = foo();
14     getchar();
15     return 0;
16 }

 

  (2)Java Program

 1 // filename Main.java
 2 public class Main 
 3 {
 4     public int foo() 
 5     {
 6         return 10;
 7     }
 8     public char foo() 
 9     {
10         // compiler error: foo() is already defined
11         return 'a';
12     }
13     public static void main(String args[])
14     { 
15     }
16 }

  

  the return type of functions is not a part of the mangled name which is generated by the compiler for uniquely identifying each function. The
  No of arguments
  Type of arguments &
  Sequence of arguments
  are the parameters which are used to generate the unique mangled name for each function. It is on the basis of these unique mangled names that compiler can understand which function to call even if the names are same(overloading).

  Hence..... i hope u have understood what i am saying.[引自网友]

  

 

  

  Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

  转载请注明:http://www.cnblogs.com/iloveyouforever/

  

原文地址:https://www.cnblogs.com/iloveyouforever/p/3442401.html