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类型的转换,在网络编程中这个算法是最基本的算法, ...
随机推荐
- 梳理源码:spring ioc容器加载的流程图
- 00002 - echo命令详解
用于字符串的输出 格式 echo string 使用echo实现更复杂的输出格式控制 1.显示普通字符串: echo "It is a test" 这里的双引号完全可以省略,以下命 ...
- linux中Centos7搭建lnmp环境
1.安装yum yum update 2.安装nginx源: yum localinstall http://nginx.org/packages/centos/7/noarch/RPMS/nginx ...
- CF865D Buy Low Sell High
/* 贪心来选择, 如果能找到比当前小的, 就用最小的来更新当前的 优先队列即可 */ #include<cstdio> #include<algorithm> #includ ...
- 插头DP模板
/* 插头dp模板 抄的GNAQ 的 括号表示法 */ #include<cstdio> #include<algorithm> #include<cstring> ...
- 剑指offer(一):二维数组中的查找
说明: 1.本系列是根据<剑指Offer>这个系列做的一个小笔记. 2.直接动力是因为师兄师姐找工作很难,而且机械出生的我面试算法更难. 3.刚开始准备刷LeetCode.LintCode ...
- Structs 2 session 学习
后台获取 值类型 request.getSession().setAttribute("username", user.getUserName()); 对象类型 reque ...
- [Unity插件]AI行为树使用总结
参考链接: https://blog.csdn.net/linxinfa/article/details/72937709 https://blog.csdn.net/wanghaodiablo/ar ...
- mysql 动态拼接表字段,值 mybatis 动态获取表字段
-- 取表所有字段,自动用逗号分开 select GROUP_CONCAT(DISTINCT COLUMN_NAME) from information_schema.columns where ta ...
- 关于lampp中的proftpd的一些使用
这个是配置文件 ServerName "ProFTPD" ServerType standalone DefaultServer on Port 21 这个是端口 Umask Ma ...