Function overloading and const keyword
Predict the output of following C++ program.
1 #include<iostream>
2 using namespace std;
3
4 class Test
5 {
6 protected:
7 int x;
8 public:
9 Test (int i):x(i)
10 {
11 }
12
13 void fun() const
14 {
15 cout << "fun() const called " << endl;
16 }
17 void fun()
18 {
19 cout << "fun() called " << endl;
20 }
21 };
22
23 int main()
24 {
25 Test t1 (10);
26 const Test t2 (20);
27 t1.fun();
28 t2.fun();
29 return 0;
30 }
Output: The above program compiles and runs fine, and produces following output.
fun() called
fun() const called
The two methods ‘void fun() const’ and ‘void fun()’ have same signature except that one is const and other is not. Also, if we take a closer look at the output, we observe that, ‘const void fun()’ is called on const object and ‘void fun()’ is called on non-const object.
C++ allows member methods to be overloaded on the basis of const type. Overloading on the basis of const type can be useful when a function return reference or pointer. We can make one function const, that returns a const reference or const pointer, other non-const function, that returns non-const reference or pointer. See this for more details.
What about parameters?
Rules related to const parameters are interesting. Let us first take a look at following two examples. The program 1 fails in compilation, but program 2 compiles and runs fine.
1 // PROGRAM 1 (Fails in compilation)
2 #include<iostream>
3 using namespace std;
4
5 void fun(const int i)
6 {
7 cout << "fun(const int) called ";
8 }
9 void fun(int i)
10 {
11 cout << "fun(int ) called " ;
12 }
13 int main()
14 {
15 const int i = 10;
16 fun(i);
17 return 0;
18 }
Output:
Compiler Error: redefinition of 'void fun(int)'
1 // PROGRAM 2 (Compiles and runs fine)
2 #include<iostream>
3 using namespace std;
4
5 void fun(char *a)
6 {
7 cout << "non-const fun() " << a;
8 }
9
10 void fun(const char *a)
11 {
12 cout << "const fun() " << a;
13 }
14
15 int main()
16 {
17 const char *ptr = "GeeksforGeeks";
18 fun(ptr);
19 return 0;
20 }
Output:
const fun() GeeksforGeeks
C++ allows functions to be overloaded on the basis of const-ness of parameters only if the const parameter is a reference or a pointer.
That is why the program 1 failed in compilation, but the program 2 worked fine. This rule actually makes sense. In program 1, the parameter ‘i’ is passed by value, so ‘i’ in fun() is a copy of ‘i’ in main(). Hence fun() cannot modify ‘i’ of main(). Therefore, it doesn’t matter whether ‘i’ is received as a const parameter or normal parameter. When we pass by reference or pointer, we can modify the value referred or pointed, so we can have two versions of a function, one which can modify the referred or pointed value, other which can not.
As an exercise, predict the output of following program.
1 #include<iostream>
2 using namespace std;
3
4 void fun(const int &i)
5 {
6 cout << "fun(const int &) called ";
7 }
8 void fun(int &i)
9 {
10 cout << "fun(int &) called " ;
11 }
12 int main()
13 {
14 const int i = 10;
15 fun(i);
16 return 0;
17 }
Output:
fun(const int &) called
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:33:28
Function overloading and const keyword的更多相关文章
- 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 in C++
In C++, following function declarations cannot be overloaded. (1)Function declarations that differ o ...
- [Javascript] Understand common misconceptions about ES6's const keyword
Values assigned with let and const are seen everywhere in JavaScript. It's become common to hear the ...
- [c++] Operator overloading
c++的操蛋属性:自己为一档,空一档,其他随意. UB_stack a; UB_stack b = a; // copy auto c = a; auto d {a}; // (or auto d = ...
- const成员函数
尽管函数名和参数列表都相同,void foo( ) const成员函数是可以与void foo( )并存的,可以形成重载! 我们假设调用语句为obj.foo(),如果obj为non-const对象,则 ...
- C: const and static keywords
原文:http://www.noxeos.com/2011/07/29/c-const-static-keywords/ C: const and static keywords Ok, once a ...
- const, static and readonly
const, static and readonly http://tutorials.csharp-online.net/const,_static_and_readonly Within a cl ...
- [ES6] 22. Const
'const' keyword is for creating a read only variable, something you can never change once created. ' ...
随机推荐
- MacOS修复TNT和谐软件运行崩溃、闪退问题
因为Apple删除了TNT的证书,因此部分应用程序出现了打开崩溃的情况. 目前的解决方案是自己更改签名. 第一种方法: 在终端中运行以下命令:(注意:name.app就是需要更改签名的程序) sudo ...
- 2021 羊城杯WriteUP
比赛感受 题目质量挺不错的,不知道题目会不会上buu有机会复现一下,躺了个三等奖,发下队伍的wp Team BinX from GZHU web Checkin_Go 源码下载下来发现是go语言写的 ...
- es date_histogram强制补零
es补零 GET /cars/transactions/_search { "size" : 0, "aggs": { "sales": { ...
- MAC电脑如何将常规视频中音频提取出来(转换格式并调整采样频率),并利用讯飞语音识别文字
1.下载好相关视频 2.选中需要提取视频,鼠标右键找到「编码所选视频文件」 3.设置中,下拉选择「仅音频」,点击继续 4.找到已提取成功的音频,鼠标右键或快捷键「command + I」,显示简介.默 ...
- vue + cesium开发(3) cesium1.87更新问题
官方在2021年11月1号更新日志中记录了他们把zip.js升级到了2.3.12以适应webpack4中的关于import.meta不兼容的语法问题,但是经过实测,1.87版本依然没有解决这个问题,所 ...
- [loj3463]表达式求值
类似cf582E,先建出表达式树,然后树形dp+离散+min和max卷积的优化,复杂度为$o(nm|E|)$,无法通过 考虑我们仅关心于这$n$个数的大小关系,具体来说,假设给出的数组是$a_{i,j ...
- [luogu5163]WD与地图
将删边改为插边,如果是无向图直接线段树合并即可,考虑如何将有向边转换为无向边 令$t_{i}$表示当插入到第$t_{i}$条边时恰好满足$x_{i}$与$y_{i}$在同一个强连通分量中,然后分类讨论 ...
- [loj3179]视觉程序
暴力做法:1.对每一行/列求$or$:2.枚举行的差值$i$,并对任意相差为$i$的行和相差为$k-i$的列求$and$,对行/列的$and$结果求$or$,对行和列的$or$求$and$,对所有$i ...
- Go语言程序结构之变量
初识Go语言之变量 var声明创建一个具体类型的变量,然后给它附加一个名字,设置他的初始值,这种声明都是一个通用的形式: var name type = expression 在实际的开发中,为了方便 ...
- CF1610F F. Mashtali: a Space Oddysey
我们首先发现有如下性质: 我们不妨先随机定向边,那么我们发现无论我们如何翻转边. 都会对其两端的点,造成 \(2 / 4\) 的影响,所以我们发现如果一个点其和他相连的所有边权和为偶数,则我们不能调整 ...