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
Function Overloading in C++的更多相关文章
- function overloading/ declare function
Declare a function To declare a function without identifying the argument list, you can do it in thi ...
- Function overloading and return type
In C++ and Java, functions can not be overloaded if they differ only in the return type. For example ...
- Function overloading and const keyword
Predict the output of following C++ program. 1 #include<iostream> 2 using namespace std; 3 4 c ...
- javascript 函数重载 overloading
函数重载 https://en.wikipedia.org/wiki/Function_overloading In some programming languages, function over ...
- C++ Knowledge series overloading
What does the compiler behind our programming? Overloading in C++ Override all of overloaded functio ...
- C++——overloading
参考 C++——overloading principle analysis operator overloading C语言中,对一个东西进行操作一定要涉及到一个函数,对于自定义类型,为了实现其四则 ...
- PostgreSQL 之 CREATE FUNCTION
官方文档 语法: CREATE [ OR REPLACE ] FUNCTION name ( [ [ argmode ] [ argname ] argtype [ { DEFAULT | = } d ...
- Javascript函数重载,存在呢—还是存在呢?
1.What's is 函数重载? );//Here is int 10 print("ten");//Here is string ten } 可以发现在C++中会根据参数的类型 ...
- [翻译]lithium 安装
安装 要求 web服务器 你需要一个web服务器来运行你的应用,最好是可以运行在你的本地机器上(你所有的开发不是都在这上面做的吗,不是吗?不是吗?).对于PHP而言,框架在很多web服务器上都运行的很 ...
随机推荐
- 攻防世界 WEB 高手进阶区 easytornado Writeup
攻防世界 WEB 高手进阶区 easytornado Writeup 题目介绍 题目考点 Python模板 tornado 模板注入 Writeup 进入题目, 目录遍历得到 /flag.txt /w ...
- VMware Workstation 无法连接到虚拟机。请确保您有权运行该程序、访问该程序使用的所有目录以及访问所有临时文件目录。 VMware Authorization Service 当前未运行
VMware Workstation 无法连接到虚拟机.请确保您有权运行该程序.访问该程序使用的所有目录以及访问所有临时文件目录. VMware Authorization Service 当前未运行 ...
- 1组-Alpha冲刺-3/6
一.基本情况 队名:震震带着六菜鸟 组长博客:https://www.cnblogs.com/Klein-Wang/p/15544334.html 小组人数:7人 二.冲刺概况汇报 王业震 过去两天完 ...
- Chrome handless无界面浏览器的脚本操作
1.什么是Phantomjs (已经停止更新) 是一个无界面的浏览器 支持页面元素查找,js的执行等 由于不进行css和gui渲染,运行效率要比真实的浏览器要快很多 2.如何使用Phantomjs? ...
- python 字符串和demical转换
转成decimal格式 import decimal a="12" #判断是否是有数字 print(a.isdecimal()) #转化成decimal.Decimal格式 a1= ...
- 卸载.net 5.0后使用dotnet提示Found .NET Core SDK
之前安装了预览版本的vs2019后试了下,然后卸载了.但发现控制台执行dotnet相关命令提示Found .NET Core SDK, but did not find dotnet.dll at [ ...
- Codeforces 891D - Sloth(换根 dp)
Codeforces 题面传送门 & 洛谷题面传送门 换根 dp 好题. 为啥没人做/yiw 首先 \(n\) 为奇数时答案显然为 \(0\),证明显然.接下来我们着重探讨 \(n\) 是偶数 ...
- Codeforces 1383C - String Transformation 2(找性质+状压 dp)
Codeforces 题面传送门 & 洛谷题面传送门 神奇的强迫症效应,一场只要 AC 了 A.B.D.E.F,就一定会把 C 补掉( 感觉这个 C 难度比 D 难度高啊-- 首先考虑对问题进 ...
- 洛谷 P3643 - [APIO2016]划艇(dp)
题面传送门 一道难度中等的 \(dp\)(虽然我没有想出来/kk). 首先一眼 \(dp_{i,j}\) 表示考虑到第 \(i\) 个学校,第 \(i\) 个学校派出了 \(j\) 个划艇的方案数,转 ...
- 洛谷 P4569 - [BJWC2011]禁忌(AC 自动机+矩阵乘法)
题面传送门 又好久没做过 AC 自动机的题了,做道练练手罢( 首先考虑对于某个固定的字符串怎样求出它的伤害,我们考虑贪心,每碰到出现一个模式串就将其划分为一段,最终该字符串的代价就是划分的次数.具体来 ...