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 ...
随机推荐
- 难顶!面试官问我G1垃圾收集器
面试官:要不这次来聊聊G1垃圾收集器? 候选者:嗯嗯,好的呀 候选者:上次我记得说过,CMS垃圾收集器的弊端:会产生内存碎片&&空间需要预留 候选者:这俩个问题在处理的时候,很有可能会 ...
- 解决IntelliJ IDEA的Plugins无法访问Marketplace去下载插件
本文图文讲解如何解决IntelliJ IDEA的Plugins无法访问Marketplace去下载插件. 默认打开IDEA的Plugins会加载很久,最后什么也没加载出来. 这时我们可以给插件市场设置 ...
- Part 33 Angular nested scopes and controller as syntax
Working with nested scopes using $scope object : The following code creates 3 controllers - country ...
- MySQL基础语句(修改)
①INSERT INSERT INTO students (class_id, name, gender, score) VALUES (2, '大牛', 'M', 80); 向students表插入 ...
- Mac下查看 Java 安装目录位置和安装数量
/usr/libexec/java_home -V 第一个红框是安装数量, 第二个红框是目前正在使用的 JDK 版本位置
- Chrome handless无界面浏览器的脚本操作
1.什么是Phantomjs (已经停止更新) 是一个无界面的浏览器 支持页面元素查找,js的执行等 由于不进行css和gui渲染,运行效率要比真实的浏览器要快很多 2.如何使用Phantomjs? ...
- CVPR 之 老照片修复
周末闲来无事,随手整理电脑里的照片,望着一张物是人非的老相片,勾起了斑驳的回忆.忽尔转念一想,何不 PS 下,但 PhotoShop 有些大且不免费自己懒得装,于是,转向免费的图像复原软件. 网上搜来 ...
- Go语言核心36讲(Go语言实战与应用十三)--学习笔记
35 | 并发安全字典sync.Map (下) 我们在上一篇文章中谈到了,由于并发安全字典提供的方法涉及的键和值的类型都是interface{},所以我们在调用这些方法的时候,往往还需要对键和值的实际 ...
- 直接插入100w数据报错
### Cause: com.mysql.cj.jdbc.exceptions.PacketTooBigException: Packet for query is too large (77,600 ...
- html+css第三篇
css reset 原则: 但凡是浏览默认的样式,都不要使用. body,dl,dd,p,h1,h2,h3,h4,h5,h6{margin:0;font-size:12px;} ol,ul{margi ...