zoukankan      html  css  js  c++  java
  • 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/

      

  • 相关阅读:
    The Game
    棋盘问题(dfs)(类似8皇后)
    dfs---之全排列
    Dijkstra算法(求单源最短路径)
    四点共面 (简单题)
    最长递增子序列
    线段的重叠
    kruskal
    hdu 1232 畅通工程
    无限对拍程序
  • 原文地址:https://www.cnblogs.com/iloveyouforever/p/3442401.html
Copyright © 2011-2022 走看看