Function overloading and return type
In C++ and Java, functions can not be overloaded if they differ only in the return type.
For example, the following program C++ and Java programs fail in compilation.
(1)C++ Program
1 #include<iostream>
2 int foo()
3 {
4 return 10;
5 }
6
7 char foo() { // compiler error; new declaration of foo()
8 return 'a';
9 }
10
11 int main()
12 {
13 char x = foo();
14 getchar();
15 return 0;
16 }
(2)Java Program
1 // filename Main.java
2 public class Main
3 {
4 public int foo()
5 {
6 return 10;
7 }
8 public char foo()
9 {
10 // compiler error: foo() is already defined
11 return 'a';
12 }
13 public static void main(String args[])
14 {
15 }
16 }
the return type of functions is not a part of the mangled name which is generated by the compiler for uniquely identifying each function. The
No of arguments
Type of arguments &
Sequence of arguments
are the parameters which are used to generate the unique mangled name for each function. It is on the basis of these unique mangled names that compiler can understand which function to call even if the names are same(overloading).
Hence..... i hope u have understood what i am saying.[引自网友]
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/
Function overloading and return type的更多相关文章
- error C2556: 'const char &MyString::operator [](int)' : overloaded function differs only by return type from 'char &MyString::operator [](int)'
char & operator[](int i);const char & operator[](int i);/*const char & operator(int i);* ...
- [TypeScript] Infer the Return Type of a Generic Function Type Parameter
When working with conditionals types, within the “extends” expression, we can use the “infer” keywor ...
- Flask - 访问返回字典的接口报错:The view function did not return a valid response. The return type must be a string, tuple, Response instance, or WSGI callable, but it was a dict.
背景 有一个 Flask 项目,然后有一个路由返回的是 dict 通过浏览器访问,结果报错 关键报错信息 TypeError: 'dict' object is not callable The vi ...
- C++:Abstract class : invalid abstract return type for member function ‘virtual...’
#include <iostream> #include <cmath> #include <sstream> using namespace std; class ...
- function overloading/ declare function
Declare a function To declare a function without identifying the argument list, you can do it in thi ...
- c++11: trailing return type in functions(函数返回类型后置)
In C++03, the return type of a function template cannot be generalized if the return type relies on ...
- Return type declarations返回类型声明
PHP 7.新增了返回类型声明 http://php.net/manual/en/functions.returning-values.php 在PHP 7.1中新增了返回类型声明为void,以及类型 ...
- ES5 function & ES6 class & method type
ES5 function & ES6 class & method type ES5 function "use strict"; /** * * @author ...
- Function Overloading in C++
In C++, following function declarations cannot be overloaded. (1)Function declarations that differ o ...
随机推荐
- 解决mac主机无法与 Docker容器互通问题
方法很多,这里我说一下使用 docker-connector解决这个问题 这是一个github开源项目docker-connector 1. Mac 通过 brew 安装 docker-connec ...
- WPF进阶技巧和实战09-事件(1-路由事件、鼠标键盘输入)
理解路由事件 当有意义的事情发生时,有对象(WPF的元素)发送的用于通知代码的消息,就是事件的核心思想.WPF通过事件路由的概念增强了.NET事件模型.事件由允许源自某个元素的事件由另一个元素引发.例 ...
- 大一C语言学习笔记(11)---编程篇--写一个程序,可以获取从键盘上输入的的三个数,并能够判断是否可以以这三个数字作为边长来构成一个三角形,如果可以的话,输出此三角形的周长及面积,要求 0 bug;
考核内容: 写一个程序,可以获取从键盘上输入的的三个数,并能够判断是否可以以这三个数字作为边长来构成一个三角形,如果可以的话,输出此三角形的周长及面积: 答案: #include<stdio.h ...
- [loj6278]数列分块入门2
做法1 以$K$为块大小分块,并对每一个块再维护一个排序后的结果,预处理复杂度为$o(n\log K )$ 区间修改时将整块打上标记,散块暴力修改并归并排序,单次复杂度为$o(\frac{n}{K}+ ...
- [atARC094D]Worst Case
首先,容易证明满足条件的$ip_{i}$必然是一个前缀 将其看成一张二分图,$i$向满足$ip_{i}<xy$的$p_{i}$连边,即找到一个前缀满足其有完美匹配 二分枚举前缀长度$k$,根据h ...
- [atAGC106F]Figures
考虑purfer序列,若生成树的pufer序列为$p_{i}$,则答案为$(\prod_{i=1}^{n}a_{i})\sum_{p}\prod_{i=1}^{n}\frac{(a_{i}-1)!}{ ...
- [bzoj1635]最高的牛
初始如果没有限制,很显然每一头牛高度都是h当只有一个限制,让h[a]到h[b]的高度都减1即可容易发现两个限制不会相交(否则必然矛盾),只会包含或相离,因此没有影响,直接差分/线段树即可(注意:1.不 ...
- 第一个vue程序
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- banner.txt
Spring Boot Version: ${spring-boot.version} __----~~~~~~~~~~~------___ . . ~~//====...... __--~ ~~ - ...
- [SQL Server]一列多行转换为字符串
在 SQL Server 中,如何将多行数据变成一个字符串保存. skill 投石 挖矿 刮痧 上面三行数据想要得到结果为:投石,挖矿,刮痧 有两种方式: 拼接字符串. 使用 for XML 首先创建 ...