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. Iceberg概述

    背景 随着大数据领域的不断发展, 越来越多的概念被提出并应用到生产中而数据湖概念就是其中之一, 其概念参照阿里云的简介: 数据湖是一个集中式存储库, 可存储任意规模结构化和非结构化数据, 支持大数据和 ...

  2. SpringCloud远程服务调用

    笔记 在微服务中,若想要使用远程调用,需要引入spring-cloud-starter-openfeign(在使用注册中心的环境下) <dependency> <groupId> ...

  3. Python 爬取 书籍

    ... import requests from bs4 import BeautifulSoup def gethtml(url,h): r = requests.get(url,headers=h ...

  4. [cf587D]Duff in Mafia

    二分最大边权,即有些边强制不能被选 接下来,即任意一点上某两边不能同时被选,以及任意一点上颜色相同的两边必须被选择一条 这些限制都可以用2-sat的形式来描述(强制不能选即连边"选-> ...

  5. 三、MapReduce编程实例

    前文 一.CentOS7 hadoop3.3.1安装(单机分布式.伪分布式.分布式 二.JAVA API实现HDFS MapReduce编程实例 @ 目录 前文 MapReduce编程实例 前言 注意 ...

  6. 雇工模式(Employee Pattern)

    本文节选自<设计模式就该这样学> 1 雇工模式的定义 雇工模式(Employee Pattern)也叫作仆人模式(Servant Pattern),属于行为型设计模式,它为一组类提供通用的 ...

  7. mybatis-参数如何测试

    mybatis参数非常多测试的时候定位bug一直是个问题,如果老用大部分时间来定位一个错误的参数,太浪费时间了...

  8. URL URI傻傻分不清楚,dart告诉你该怎么用

    目录 简介 dart中的URI encode和decode 解析URI 总结 简介 如果我们要访问一个网站,需要知道这个网站的地址,网站的地址一般被称为URL,他的全称是Uniform Resourc ...

  9. 阿里性能专家全方位对比Jmeter和Locust,到底谁更香?

    近些年,随着互联网行业的不断发展,用户规模也有了爆发性的增长.产品的性能成为影响用户体验的重要因素.因此,性能测试越来越受到大型互联网企业的重视. 在做性能测试时,通常都会借助一些压测工具来模拟大量的 ...

  10. HDU 6755 - Fibonacci Sum(二项式定理+推式子)

    题面传送门 其实是一道还好的题罢,虽然做了我 2147483647(bushi,其实是 1.5h),估计也只是因为 HDU 不支持数据下载所以错误总 debug 出来 首先看到 \(10^9+9\) ...