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

Function Overloading in C++的更多相关文章

  1. function overloading/ declare function

    Declare a function To declare a function without identifying the argument list, you can do it in thi ...

  2. Function overloading and return type

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

  3. Function overloading and const keyword

    Predict the output of following C++ program. 1 #include<iostream> 2 using namespace std; 3 4 c ...

  4. javascript 函数重载 overloading

    函数重载 https://en.wikipedia.org/wiki/Function_overloading In some programming languages, function over ...

  5. C++ Knowledge series overloading

    What does the compiler behind our programming? Overloading in C++ Override all of overloaded functio ...

  6. C++——overloading

    参考 C++——overloading principle analysis operator overloading C语言中,对一个东西进行操作一定要涉及到一个函数,对于自定义类型,为了实现其四则 ...

  7. PostgreSQL 之 CREATE FUNCTION

    官方文档 语法: CREATE [ OR REPLACE ] FUNCTION name ( [ [ argmode ] [ argname ] argtype [ { DEFAULT | = } d ...

  8. Javascript函数重载,存在呢—还是存在呢?

    1.What's is 函数重载? );//Here is int 10 print("ten");//Here is string ten } 可以发现在C++中会根据参数的类型 ...

  9. [翻译]lithium 安装

    安装 要求 web服务器 你需要一个web服务器来运行你的应用,最好是可以运行在你的本地机器上(你所有的开发不是都在这上面做的吗,不是吗?不是吗?).对于PHP而言,框架在很多web服务器上都运行的很 ...

随机推荐

  1. Linux下软链接与硬链接的区别

    由于下面会说到inode,所以如果没有了解过,请务必搞懂inode的真正含义,厚颜无耻的推荐我的一篇博客:Linux磁盘与文件系统管理 如果我们在系统中新建一个文件,我们看到的文件名实际上只是表面现象 ...

  2. ELK集群之filebeat(6)

    filebeat工作原理 ilebeat是本地文件的日志数据采集器. 作为服务器上的代理安装,Filebeat监视日志目录或特定日志文件,tail file,并将它们转发给Elasticsearch或 ...

  3. dart系列之:dart语言中的函数

    目录 简介 函数的参数 main函数 匿名函数 闭包 函数的返回值 总结 简介 函数是所有编程语言都有的内容,不管是面向对象还是面向过程,函数都是非常重要的一部分.dart中的函数和java中的函数有 ...

  4. 用 python 解决线性代数中的矩阵运算

    用 python 解决线性代数中的矩阵运算 矩阵叉乘 矩阵求逆 矩阵转置 假定AX=B,求解未知矩阵X 矩阵的行列式值|matrix| 未完待续..... import sys from PyQt5. ...

  5. Express 的基本使用(创建一个简单的服务器)

    Express 的基本使用(创建一个简单的服务器) const express = require('express') // 创建服务器应用程序 // 相当于 http.creatServer co ...

  6. LeetCode 199. 二叉树的右视图 C++ 用时超100%

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...

  7. 个人网站迁移之旅:从博客到知识库,从 Hexo 到 Docusaurus

    或是出于跟风,或是为了简历能好看点,2020 年 2 月,在翻看了中文互联网大量的「免费个人网页搭建教程」后,我选择了 Hexo + Github Pages 的方案,找了一款看上去还不错的主题,搭建 ...

  8. 看动画学算法之:hashtable

    目录 简介 散列表的关键概念 数组和散列表 数组的问题 hash的问题 线性探测 二次探测 双倍散列 分离链接 rehash 简介 java中和hash相关并且常用的有两个类hashTable和has ...

  9. [luogu4107]兔子和樱花

    有两个贪心:1.自底向上dfs,能删就删,这样显然是正确的,因为它最多只会造成它父亲不能删除:2.对于一个节点,优先删除其代价($c[i]+son[i]$)最大的i删除,一定最优,证明略 1 #inc ...

  10. 通过 for 循环,比较 Python 与 Ruby 编程思想的差别

    作者:Doug Turnbull 译者:豌豆花下猫@Python猫 原文:https://softwaredoug.com/blog/2021/11/12/ruby-vs-python-for-loo ...