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. 谷粒 | 11 | nginx windows版简单安装使用

    nginx配置 下载安装 传送门:官网下载 官网提供三种版本: Mainline version:Mainline 是 Nginx 目前主力在做的版本,可以说是开发版 Stable version:最 ...

  2. oxidized备份华为HRP防火墙配置失败问题

    Oxidized备份华为防火墙配置Last Status红色,备份失败,查看oxidized日志(默认是~/.config/oxidized/logs/oxidized.log)能看到报错日志: WA ...

  3. Django 小实例S1 简易学生选课管理系统 0 初步介绍与演示

    Django 小实例S1 简易学生选课管理系统 第0章--初步介绍与演示 点击查看教程总目录 作者自我介绍:b站小UP主,时常直播编程+红警三,python1对1辅导老师. 1 初步介绍 先介绍下这个 ...

  4. Go语言核心36讲(Go语言实战与应用五)--学习笔记

    27 | 条件变量sync.Cond (上) 前导内容:条件变量与互斥锁 我们常常会把条件变量这个同步工具拿来与互斥锁一起讨论.实际上,条件变量是基于互斥锁的,它必须有互斥锁的支撑才能发挥作用. 条件 ...

  5. es使用java的api操作

    基本环境的创建 pom依赖  <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&q ...

  6. Nocalhost 为 KubeSphere 提供更强大的云原生开发环境

    作者简介 张海立(驭势科技云平台研发总监):开源爱好者,云原生社区上海站 PMC 成员,KubeSphere Ambassador:日常云原生领域工作涉及 Kubernetes.DevOps.可观察性 ...

  7. ROS机器人开发实践1->SSH远程登录要点记录

    1.有线网卡 设置 找到有线网络,点击设置,修改其中的IPv4的地址和子网掩码. 1 //地址 2 192.168.xxx.xxx 3 //子网掩码 4 255.255.255.0 点击应用 2.配置 ...

  8. nvm安装以及管理多版本node教程

    安装nvm.node.npm 下载nvm安装包,推荐使用1.1.7,我个人使用1.1.8会有中文乱码的报错 点击exe文件,注意修改nvm的安装根目录以及node的安装根目录,后者是以后管理多版本no ...

  9. 【CTSC1999】【带权并查集 】月亮之眼

    Description 吉儿是一家古董店的老板娘,由于她经营有道,小店开得红红火火.昨天,吉儿无意之中得到了散落民间几百年的珍宝-月亮之眼.吉儿深知"月亮之眼"价值连城:它是由许多 ...

  10. 贪心/构造/DP 杂题选做Ⅲ

    颓!颓!颓!(bushi 前传: 贪心/构造/DP 杂题选做 贪心/构造/DP 杂题选做Ⅱ 51. CF758E Broken Tree 讲个笑话,这道题是 11.3 模拟赛的 T2,模拟赛里那道题的 ...