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. 设计模式学习-使用go实现原型模式

    原型模式 定义 代码实现 优点 缺点 适用场景 参考 原型模式 定义 如果对象的创建成本比较大,而同一个类的不同对象之间差别不大(大部分字段都相同),在这种情况下,我们可以利用对已有对象(原型)进行复 ...

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

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

  3. JMeter学习笔记--JDBC测试计划-连接Mysql

    1.首先要下载jar包,mysql-connector-java-5.1.7-bin.jar 放到Jmeter的lib文件下ext下 2.添加JDBC Connection Configuration ...

  4. 关于 better-scroll 设置了以后无法滚动或不生效的问题

    首先在mounted里面注册组件  例:let scroll = new BScroll("#commondityLeftList")   然后打印实例化对象,例:console. ...

  5. webpack 打包图片资源

    webpack 打包图片资源 /** * loader: 1. 下载 2. 使用(配置) * plugins:1. 下载 2. 引入 3.使用 */ // 用来拼接绝对路径的方法 const {res ...

  6. PTA 银行排队问题之单队列多窗口服务 (25分)

    PTA 银行排队问题之单队列多窗口服务 (25分) 假设银行有K个窗口提供服务,窗口前设一条黄线,所有顾客按到达时间在黄线后排成一条长龙.当有窗口空闲时,下一位顾客即去该窗口处理事务.当有多个窗口可选 ...

  7. [JavaScript] 实现简单的表单数据校验功能

    实现表单数据校验功能 因为项目用的UI库功能太少,表单不具备校验功能,所以自己写了一个,只有一个文件. 使用 import { required, email, useValidate } from ...

  8. Debug代码调试

    Debug代码调试 第一步在代码左侧先点一个红点 第二步右键选择Debug运行 第三步点击Step Into按键分步进行 练习题: s2 = 'python python python python ...

  9. [atARC107F]Sum of Abs

    价值即等价于给每一个点系数$p_{i}=\pm 1$,使得$\forall (x,y)\in E,p_{x}=p_{y}$的最大的$\sum_{i=1}^{n}p_{i}b_{i}$ 如果没有删除(当 ...

  10. [hdu6761]Minimun Index

    $lyndon\ word$(以下简写为Lw):对于一个字符串s,其为Lw当且仅当其的最小后缀为自身 性质:若$u<v$为LW,那么$uv$也为Lw(反证法即可证) $lyndon$分解:将一个 ...