C++中 int i 与 int &i 注意事项
来源:http://blog.csdn.net/qianchenglenger/article/details/16949689
1.int i 传值,int & i 传引用
int i不会回带参数,而int &i可以回带参数,如
- #include <iostream>
- void test1(int i)
- {
- i = 7;
- }
- void test2(int &i) //要限制参数改动,可以加const限制
- {
- i = 7;
- }
- int main()
- {
- int t1 = 10;
- test1(t1);
- std::cout << t1 << std::endl; //输出为10
- int t2 = 10;
- test2(t2);
- std::cout << t2 << std::endl; //输出为7
- return 0;
- }
2. int i 可赋予常量,而int & i 不能
- #include <iostream>
- void test1(int i)
- {
- i = 7;
- }
- void test2(int &i)
- {
- i = 7;
- }
- int main()
- {
- int i = 10; //合法
- int &i1 = 10; //编译错误
- test1(10); //合法
- test2(10); //编译错误
- return 0;
- }
3. int &i 相当于别名,而int i 只是拷贝
- #include <iostream>
- int main()
- {
- int t1 = 10;
- int t2 = 10;
- int i1 = t1; //复制
- int &i2 = t2; //别名
- i1 = 7;
- i2 = 7;
- std::cout << t1 << std::endl; //输出10
- std::cout << t2 << std::endl; //输出7
- return 0;
- }
最后,我们再来看一下个例子
- #include <iostream>
- class A{
- public:
- A(int a, int b):i1(a),i2(b){};
- public:
- int i1;
- int &i2;
- };
- int main()
- {
- A a(45,60);
- std::cout << a.i1 << " " << a.i2 << std::endl;
- return 0;
- }
在电脑上运行之后,你会发现,第一个数字正常,而第二个数字明显是一个未定义的值,例如我运行后得到的结果是
45 1400458944
这是因为我们在构造一个对象的时候,调用了构造函数,而A的构造函数的参数传递为传值方式,所以,当调用时,相当于有一个
- int b = 60
- int &i2 = b;
而当构造函数调用完成,b的作用域结束,b被销毁,而i2指向一个已经被销毁的地方,所以会出现未定义的运行结果。
我们再贴一段程序,和上面的一段相对应,只是,这次,我们将会获得 45 60 的结果
- #include <iostream>
- class A{
- public:
- A(int a, int &b):i1(a),i2(b){};
- public:
- int i1;
- int &i2;
- };
- int main()
- {
- int t = 60;
- A a(45,t);
- std::cout << a.i1 << " " << a.i2 << std::endl;
- return 0;
- }
C++中 int i 与 int &i 注意事项的更多相关文章
- C#中(int)、int.Parse()、int.TryParse()和Convert.ToInt32()的区别
转自:http://www.cnblogs.com/leolis/p/3968943.html 在编程过程中,数据转换是经常要用到的,C#中数据转换的方法很多,拿将目标对象转换为 整型(int)来讲, ...
- C#中(int)、int.Parse()、int.TryParse()和Convert.ToInt32()的区别 <转>
作者:Statmoon 出处:http://leolis.cnblogs.com/ 在编程过程中,数据转换是经常要用到的,C#中数据转换的方法很多,拿将目标对象转换为整型(int)来讲,有四种方法 ...
- MySql中的tinying,smallint,int,bigint的类型介绍——转载
tinyint 从 0 到 255 的整型数据.存储大小为 1 字节. smallint 从 -2^15 (-32,768) 到 2^15 – 1 (32,767) 的整型数据.存储大小为 2 个字节 ...
- C#/C++ 中字节数组与int类型转换
1.C#中int和byte[]转换: /// <summary> /// 把int32类型的数据转存到4个字节的byte数组中 /// </summary> /// <p ...
- 嵌入式中的 *(volatile unsigned int *)0x500 解释
C语言中*(volatile unsigned int *)0x500的解释: 如下: (unsigned int *)0x500:将地址0x500强制转化为int型指针*(unsigned int ...
- java中字符串String 转 int(转)
java中字符串String 转 int String -> int s="12345"; int i; 第一种方法:i=Integer.parseInt(s); 第二种方法 ...
- MVC Action,Service中筛选int 和list<int>
action: public ActionResult DeleteByID(int id) { this.MessageService.DeleteMailTemplate(id); var fro ...
- c++ 中关于int,unsigned int , short的关系与应用
转载:http://www.cppblog.com/xyjzsh/archive/2010/10/20/130554.aspx?opt=admin int类型比较特殊,具体的字节数同机器字长和编译 ...
- [转载] java中byte数组与int,long,short间的转换
文章转载自http://blog.csdn.net/leetcworks/article/details/7390731 package com.util; /** * * <ul> * ...
- 【转】java中byte数组与int类型的转换(两种方式)----不错
原文网址:http://blog.csdn.net/piaojun_pj/article/details/5903009 java中byte数组与int类型的转换,在网络编程中这个算法是最基本的算法, ...
随机推荐
- MySql 索引 查询 优化
官方文档: https://dev.mysql.com/doc/refman/5.7/en/explain-output.html#explain_rows type: 连接类型 system 表只有 ...
- 通俗理解cookies,sessionStorage,localStorage的区别
sessionStorage .localStorage 和 cookie 之间的区别共同点:都是保存在浏览器端,且同源的. 区别:cookie数据始终在同源的http请求中携带(即使不需要),即co ...
- uva-529-枚举
题意: a0,a1,a2,a3....an 对于任意的i,j,k 0<=k<=n 0<=i<=k-1 0<=j<=k-1 ak=ai+aj 求a0.....an 解 ...
- 关于微信支付接口,curl错误代码58
微信支付接口,curl错误代码58 之前的微信付款到用户零钱都是好好的,今天运营来找我, 我想了了下,就是进行了网站搬家 看了下 微信支付相关的证书配置文件 知道了,在这个 要改下证书的路径 WxPa ...
- 16.Ubuntu安装mysql及win7安装mysql
Ubuntu: 链接地址:https://www.cnblogs.com/logaa/p/6791819.html win7: 链接地址:https://jingyan.baidu.com/artic ...
- gzip0
但是Apache是专门为PHP所匹配的,其兼容性最好),类似于IIS.下面我们具体来说说Apache里怎么启用gzip压缩: Apache启用gzip 如果要开启gzip的话,一定要打开下面二个模块. ...
- journalctl
systemd 提供了自己的日志系统(logging system),称为 journal.使用 systemd 日志,无需额外安装日志服务(syslog).读取日志的命令: # journalctl ...
- mybatis 3.2.2_环境搭建
1.创建一个工程 utf-8 2.导入jar mybatis-3.2.2.jar 核心包 依赖包: asm-3.3.1.jar cglib-2.2.2.jar commons-logging-1.1. ...
- leetcode1008
class Solution: def __init__(self): self.root = None def construct(self,order,root,i): if i==len(ord ...
- Linux 压缩归档
压缩 压缩工具:gzip bzip2 zip tar 压缩格式 常见的压缩格式:gz.bz2.xz.zip.Z tar.gz格式 [root@xuegod72 mnt]# tar zcf gr ...