Predict the output of the below code snippet.

 1 #include <iostream>
2 using namespace std;
3
4 int i;
5
6 class A
7 {
8 public:
9 ~A()
10 {
11 i=10;
12 }
13 };
14
15 int foo()
16 {
17 i=3;
18 A ob;
19 return i;
20 }
21
22 int main()
23 {
24 cout << "i = " << foo() << endl;
25 return 0;
26 }

  Output of the above program is “i = 3″.
  Why the output is i= 3 and not 10?
  While returning from a function, destructor is the last method to be executed. The destructor for the object “ob” is called after the value of i is copied to the return value of the function. So, before destructor could change the value of i to 10, the current value of i gets copied & hence the output is i = 3.

  

  How to make the program to output “i = 10″ ?
  Following are two ways of returning updated value:

  1) Return by Reference:
  Since reference gives the l-value of the variable,by using return by reference the program will output “i = 10″.

 1 #include <iostream>
2 using namespace std;
3
4 int i;
5
6 class A
7 {
8 public:
9 ~A()
10 {
11 i = 10;
12 }
13 };
14
15 int& foo()
16 {
17 i = 3;
18 A ob;
19 return i;
20 }
21
22 int main()
23 {
24 cout << "i = " << foo() << endl;
25 return 0;
26 }

  The function foo() returns the l-value of the variable i. So, the address of i will be copied in the return value.

  Since, the references are automatically dereferened. It will output “i = 10″.

  2) Create the object ob in a block scope

 1 #include <iostream>
2 using namespace std;
3
4 int i;
5
6 class A
7 {
8 public:
9 ~A()
10 {
11 i = 10;
12 }
13 };
14
15 int foo()
16 {
17 i = 3;
18 {
19 A ob;
20 }
21 return i;
22 }
23
24 int main()
25 {
26 cout << "i = " << foo() << endl;
27 return 0;
28 }

  Since the object ob is created in the block scope, the destructor of the object will be called after the block ends, thereby changing the value of i to 10. Finally 10 will copied to the return value.

  This article is compiled by Aashish Barnwal and reviewed by GeeksforGeeks team.

  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-26  11:06:08

最新文章

  1. SQL 2014 AlwaysOn 搭建
  2. Matrix Factorization SVD 矩阵分解
  3. 【收藏】Java多线程/并发编程大合集
  4. 创建第一个ArcGIS API for Silverlight应用
  5. win7 dos命令窗口内容显示不全解决办法--将命令执行结果输出到一个文件中
  6. HDU1358:Period
  7. Maven学习小结(七 生命周期[转])
  8. runnable和thread的区别
  9. ExtJS智能提示工具spket安装与破解
  10. JS属性读写操作+if判断注意事项
  11. shell统计
  12. Android Studio利用异步任务AsyncTask发送post请求获取json数据
  13. message:GDI+ 中发生一般性错误。
  14. forward reference前向引用,gloal values and local values全局变量和局部变量,recursive function递归函数
  15. tomcat-maven-plugin的使用
  16. BZOJ3632:外太空旅行(最大团,DFS)
  17. V-rep学习笔记:机器人模型创建1—模型简化
  18. Python API快餐教程(1) - 字符串查找API
  19. iOS线程开发小结
  20. linq to sql 查找所有开票金额大于回款金额的项目

热门文章

  1. c++ IO库
  2. 羽夏看Win系统内核——系统调用篇
  3. mybatis-plus查询指定字段
  4. 添加su权限
  5. CentOS7 hadoop3.3.1安装(单机分布式、伪分布式、分布式)
  6. 一个开源的C#和cefsharp项目:逐浪字体大师pc版上线(附源码开源)
  7. psutil模块详解
  8. 一文详解MySQL的锁机制
  9. I.MX启动方式和头部
  10. PaintHouse II