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. CTF-Tools 一款CTF古典密码加解密工具

    CTF-Tools 一款CTF古典密码加解密工具 工具截图 工具简介 一款CTF编码.解码.加密.解密工具. 支持的编码解码: URL-UTF-8 URL-GB2312 Unicode Escape( ...

  2. Maven 问题 Failure to transfer org.apache.maven.plugins:maven-surefire-plugin:pom:3.0.0-M1 的处理

    一.问题描述 Maven项目报错,该项目是导入的项目,然后再通过开发工具打开项目时,pom.xml文件报错. 并且新建Maven Project 也会报错. 二.报错详细Failure to tran ...

  3. 自定义实例默认值 axios.create(config)

    自定义实例默认值 axios.create(config) 根据指定配置创建一个新的axios,也就就每个新 axios 都有自己的配置 新 axios只是没有取消请求和批量发请求的方法,其它所有语法 ...

  4. django的增删改查

    前置条件: 已有一个model (tbl_user) ,用户表 1.查询 # 查询用户表 username是cx的数据 user_object = tbl_user.objects.filter(us ...

  5. Nginx server_name翻译

    http://nginx.org/en/docs/http/server_names.html#regex_names 匹配优先顺序 精确名称,无通配符,无正则. 以星号开头的最长的通配符名称,例如& ...

  6. [bzoj1483]梦幻布丁

    对于每一个颜色用一个链表存储,并记录下:1.当前某种颜色的真实颜色:2.这种颜色的数量(用于启发式合并的判断):3.当前答案(即有几段),然后对于每一个操作简单处理一下就行了. 1 #include& ...

  7. 【Microsoft Azure 的1024种玩法】六、使用Azure Cloud Shell对Linux VirtualMachines 进行生命周期管理

    [文章简介] Azure Cloud Shell 是一个用于管理 Azure 资源的.可通过浏览器访问的交互式经验证 shell. 它使用户能够灵活选择最适合自己工作方式的 shell 体验,本篇文章 ...

  8. JdbcTemplate 、Mybatis、ORM 、Druid 、HikariCP 、Hibernate是什么?它们有什么关系?

    JdbcTemplate .Mybatis.ORM .Druid .HikariCP .Hibernate是什么?它们有什么关系? 学完Spring和SpringMVC之后,就急于求成的开始学习起Sp ...

  9. C#.NET 操作Windows服务(安装、卸载)

    注意点: 1.安装时要请求到管理员权限. 2.卸载前,一定要停止掉Windows服务,否则需要重启或注销电脑.代码无法停止服务时,使用services.msc来停止. 开始: 1.新建一个名为&quo ...

  10. Identity Server 4 从入门到落地(二)—— 理解授权码模式

    Identity Server 的目的是认证和授权,我们需要理解认证和授权的工作过程.这就需要了解OAuth 2.0的四种授权模式,下面这张图是授权码模式的工作过程,至少我在一开始看得一头雾水: 现在 ...