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服务器上都运行的很 ...
随机推荐
- C 数组类型语法总结
数组类型语法总结 数组指针 和 指针数组 区分 数组指针是一个指针,只对应类型的数组.指针数组是一个数组,其中每个元素都是指针 数组指针遵循指针运算法则.指针数组拥有c语言数组的各种特性 数组类型重命 ...
- 为何我中断执行的线程不起作用,Why
摘要:我们就以一个案例的形式,来为大家详细介绍下为何中断执行的线程不起作用. 本文分享自华为云社区<明明中断了线程,却为何不起作用呢?>,作者:冰 河. 当我们在调用Java对象的wait ...
- 『学了就忘』Linux基础命令 — 32、压缩和解压缩相关命令
目录 1.".zip"格式压缩 2.".gz"格式压缩 3.".bz2"格式压缩 4.".tar"格式打包 5.打包和压 ...
- Json跨域登录问题的之Access-Control-Allow-Origin 站点跨域请求的问题
跨域调用json问题 闲暇之时,做了一个博客站点,站点发布网络之后程序功能完成,最后发现了一个跨域的问题,比如我使用abc.com打开系统,一切正常,后台没有任何文件请求报错问题,然后我又使用了www ...
- Linux之间的文件传输方式
大数据集群经常涉及文件拷贝,我在学习大数据时总结了几种方式 三台主机:192.168.10.100.192.168.10.101.192.168.10.102有一个一样的用户:swcode 做过映射关 ...
- R数据分析:纵向数据如何做中介,交叉滞后中介模型介绍
看似小小的中介,废了我好多脑细胞,这个东西真的不简单,从7月份有人问我,我多重中介,到现在的纵向数据中介,从一般的回归做法,到结构方程框架下的路径分析法,到反事实框架做法,从中介变量和因变量到是连续变 ...
- java配置方法
1.新建一个Config文件夹 2.代码 package com.shao.config; import com.shao.pojo.User; import org.springframework. ...
- C/C++ Qt TableDelegate 自定义代理组件
TableDelegate 自定义代理组件的主要作用是对原有表格进行调整,例如默认情况下Table中的缺省代理就是一个编辑框,我们只能够在编辑框内输入数据,而有时我们想选择数据而不是输入,此时就需要重 ...
- 【POJ3614 Sunscreen】【贪心】
题面: 有c头牛,需要的亮度在[min_ci,max_ci]中,有n种药,每种m瓶,可以使亮度变为v 问最多能满足多少头牛 算法 我们自然考虑贪心,我们首先对每头牛的min进行排序,然后对于每种药,将 ...
- [Cnoi2020]线性生物
期望入门题.但是我不会做. 考虑设\(E_{x\to{x+1}}\)为\(x\)到\(x+1\)点的期望步数. 则\(ans = \sum_{i = 0}^{n} E_{x\to{x+1}}\) 知\ ...