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

Playing with Destructors in C++的更多相关文章

  1. (转) Playing FPS games with deep reinforcement learning

    Playing FPS games with deep reinforcement learning 博文转自:https://blog.acolyer.org/2016/11/23/playing- ...

  2. (转) Deep Reinforcement Learning: Playing a Racing Game

    Byte Tank Posts Archive Deep Reinforcement Learning: Playing a Racing Game OCT 6TH, 2016 Agent playi ...

  3. Codeforces Round #184 (Div. 2) E. Playing with String(博弈)

    题目大意 两个人轮流在一个字符串上删掉一个字符,没有字符可删的人输掉游戏 删字符的规则如下: 1. 每次从一个字符串中选取一个字符,它是一个长度至少为 3 的奇回文串的中心 2. 删掉该字符,同时,他 ...

  4. ifrog-1028 Bob and Alice are playing numbers(trie树)

    题目链接: Bob and Alice are playing numbers DESCRIPTION Bob and his girl friend are playing game togethe ...

  5. 论文笔记之:Playing for Data: Ground Truth from Computer Games

    Playing for Data: Ground Truth from Computer Games ECCV 2016 Project Page:http://download.visinf.tu- ...

  6. 论文笔记之:Playing Atari with Deep Reinforcement Learning

    Playing Atari with Deep Reinforcement Learning <Computer Science>, 2013 Abstract: 本文提出了一种深度学习方 ...

  7. (C++) Interview in English. - Constructors/Destructors

    Constructors/Destructors. 我们都知道,在C++中建立一个类,这个类中肯定会包括构造函数.析构函数.复制构造函数和重载赋值操作:即使在你没有明确定义的情况下,编译器也会给你生成 ...

  8. CodeForces 176C Playing with Superglue 博弈论

    Playing with Superglue 题目连接: http://codeforces.com/problemset/problem/176/C Description Two players ...

  9. Playing with ptrace, Part II

    Playing with ptrace, Part II Issue From Issue # December Dec , By Pradeep Padala inSysAdmin In Part ...

随机推荐

  1. robot_framewok自动化测试--(6)Collections 库

    Collections 库 Collections 库同样为 Robot Framework 标准类库,它所提供的关键字主要用于列表.索引.字典的处理. 在使用之前需要在测试套件(项目)中添加: 1. ...

  2. 【Python+postman接口自动化测试】(7)Postman 的使用教程

    Postman v6的使用 Postman: 简单方便的接口调试工具,便于分享和协作.具有接口调试,接口集管理,环境配置,参数化,断言,批量执行,录制接口,Mock Server, 接口文档,接口监控 ...

  3. loadrunner12自带的机票预订服务,解决httpd: Could not reliably determine the server's fully qualified domain name 问题

    遇到以上问题是在启动loadrunner12自带的机票预订服务器情况下遇到的,错误提示如下图: 解决方案: 编辑httpd.conf 文件,加入一句 ServerName localhost:1080 ...

  4. js实现一个小游戏(飞翔的jj)

    js实现一个小游戏(飞翔的jj) 源代码+素材图片在我的仓库 <!DOCTYPE html> <html lang="en"> <head> & ...

  5. Spark面试题(七)——Spark程序开发调优

    Spark系列面试题 Spark面试题(一) Spark面试题(二) Spark面试题(三) Spark面试题(四) Spark面试题(五)--数据倾斜调优 Spark面试题(六)--Spark资源调 ...

  6. 国内首家!腾讯云正式成为 FinOps 基金会顶级会员

    11月24日,腾讯云正式宣布加入FinOps基金会,作为国内首家FinOps基金会顶级会员,腾讯云将联合FinOps基金会,全面推进对FinOps标准和最佳实践的贡献,为企业提供云财务管理的最佳解决方 ...

  7. 【JavaSE】类与类的关系--UML

    类(对象/接口)之间的关系 -- UML类图展现 2019-07-14  14:37:19  by冲冲 在面向对象程序设计时,类与类之间的关系主要分为:继承,实现,依赖,关联,聚合,组合等6种关系. ...

  8. myeclipse激活、破解教程

    myeclipse安装注意事项 首先要下载jdk 配置jdk的环境变量 如果1和2 都打不开说明没有下载jdk文件,点击下载就可以了,点击1的时候会出现要求下载的界面,直接下载就可以了...下载完成之 ...

  9. seq生成格式化字符

    [root@ansz.quan.bbs ~]$seq -s "+" 10 1+2+3+4+5+6+7+8+9+10 seq为生成数字序列 -s 分隔符

  10. Linux openssl 升级、降级

    Linux openssl 升级.降级 最近遇到一些朋友使用微信退款,报openssl版本为问题,需要对openssl进行降级. 现在环境的openssl版本如下: root@c215a2b695ef ...