• Function Overloading in C++


     

      In C++, following function declarations cannot be overloaded.

      (1)Function declarations that differ only in the return type.

      For example, the following program fails in compilation.

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

     

      (2)Member function declarations with the same name and the name parameter-type-list cannot be overloaded if any of them is a static member function declaration.

      For example, following program fails in compilation.

     1 #include<iostream>
     2 class Test 
     3 {
     4    static void fun(int i) 
     5    {
     6    }
     7    void fun(int i) 
     8    {
     9    }   
    10 };
    11  
    12 int main()
    13 {
    14    Test t;
    15    getchar();
    16    return 0;
    17 }

     

      (3) Parameter declarations that differ only in a pointer * versus an array [] are equivalent.

      That is, the array declaration is adjusted to become a pointer declaration. Only the second and subsequent array dimensions are significant in parameter types. For example, following two function declarations are equivalent.

    1 int fun(int *ptr);
    2 int fun(int ptr[]); // redeclaration of fun(int *ptr)

     

      (4) Parameter declarations that differ only in that one is a function type and the other is a pointer to the same function type are equivalent.

    1 void h(int ());
    2 void h(int (*)()); // redeclaration of h(int())

     

      (5) Parameter declarations that differ only in the presence or absence of const and/or volatile are equivalent.

      That is, the const and volatile type-specifiers for each parameter type are ignored when determining which function is being declared, defined, or called. For example, following program fails in compilation with error “redefinition of `int f(int)’ “

      Example:

     1 #include<iostream>
     2 #include<stdio.h>
     3   
     4 using namespace std;
     5   
     6 int f ( int x) 
     7 {
     8     return x+10;
     9 }
    10  
    11 int f ( const int x) 
    12 {
    13     return x+10;
    14 }
    15  
    16 int main() 
    17 {     
    18   getchar();
    19   return 0;
    20 }

      Only the const and volatile type-specifiers at the outermost level of the parameter type specification are ignored in this fashion; const and volatile type-specifiers buried within a parameter type specification are significant and can be used to distinguish overloaded function declarations.

      In particular, for any type T, “pointer to T,” “pointer to const T,” and “pointer to volatile T” are considered distinct parameter types, as are “reference to T,” “reference to const T,” and “reference to volatile T.”

     

      (6)Two parameter declarations that differ only in their default arguments are equivalent.

      For example, following program fails in compilation with error “redefinition of `int f(int, int)’ “

     1 #include<iostream>
     2 #include<stdio.h>
     3   
     4 using namespace std;
     5   
     6 int f ( int x, int y) 
     7 {
     8     return x+10;
     9 }
    10  
    11 int f ( int x, int y = 10) 
    12 {
    13     return x+y;
    14 }
    15  
    16 int main() 
    17 {     
    18   getchar();
    19   return 0;
    20 }

     

      

      References: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1905.pdf

     

     

      

      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/

      2013-11-25  22:14:58

  • 相关阅读:
    Leetcode 2047. 句子中的有效单词数(可以,已解决)
    Leetcode 1979. 找出数组的最大公约数
    Leetcode 1967. 作为子字符串出现在单词中的字符串数目
    Leetcode 2048. 下一个更大的数值平衡数(有点意思,已解决)
    tensorflow 自动求导
    WeisfeilerLehman(WL) 算法和WL Test
    sklearn的train_test_split() 各函数参数含义解释(非常全)
    Lesson1——Tensor
    numpy 中 * 和 np.dot() 的区别
    tf.convert_to_tensor()函数的使用 | 数据类型转换
  • 原文地址:https://www.cnblogs.com/iloveyouforever/p/3442345.html
Copyright © 2020-2023  润新知