来源:http://blog.csdn.net/qianchenglenger/article/details/16949689

1.int i 传值,int & i 传引用

int i不会回带参数,而int &i可以回带参数,如

  1. #include <iostream>
  2. void test1(int i)
  3. {
  4. i = 7;
  5. }
  6. void test2(int &i) //要限制参数改动,可以加const限制
  7. {
  8. i = 7;
  9. }
  10. int main()
  11. {
  12. int t1 = 10;
  13. test1(t1);
  14. std::cout << t1 << std::endl; //输出为10
  15. int t2 = 10;
  16. test2(t2);
  17. std::cout << t2 << std::endl;   //输出为7
  18. return 0;
  19. }

2. int i 可赋予常量,而int & i 不能

  1. #include <iostream>
  2. void test1(int i)
  3. {
  4. i = 7;
  5. }
  6. void test2(int &i)
  7. {
  8. i = 7;
  9. }
  10. int main()
  11. {
  12. int i = 10;     //合法
  13. int &i1 = 10;   //编译错误
  14. test1(10);      //合法
  15. test2(10);      //编译错误
  16. return 0;
  17. }

3. int &i 相当于别名,而int i 只是拷贝

  1. #include <iostream>
  2. int main()
  3. {
  4. int t1 = 10;
  5. int t2 = 10;
  6. int i1 = t1;    //复制
  7. int &i2 = t2;   //别名
  8. i1 = 7;
  9. i2 = 7;
  10. std::cout << t1 << std::endl;   //输出10
  11. std::cout << t2 << std::endl;   //输出7
  12. return 0;
  13. }

最后,我们再来看一下个例子

  1. #include <iostream>
  2. class A{
  3. public:
  4. A(int a, int b):i1(a),i2(b){};
  5. public:
  6. int i1;
  7. int &i2;
  8. };
  9. int main()
  10. {
  11. A a(45,60);
  12. std::cout << a.i1 << " " << a.i2 << std::endl;
  13. return 0;
  14. }

在电脑上运行之后,你会发现,第一个数字正常,而第二个数字明显是一个未定义的值,例如我运行后得到的结果是

45  1400458944

这是因为我们在构造一个对象的时候,调用了构造函数,而A的构造函数的参数传递为传值方式,所以,当调用时,相当于有一个

  1. int b = 60
存在,而 i2(b) 相当于将

  1. int &i2 = b;

而当构造函数调用完成,b的作用域结束,b被销毁,而i2指向一个已经被销毁的地方,所以会出现未定义的运行结果。
我们再贴一段程序,和上面的一段相对应,只是,这次,我们将会获得 45 60  的结果

  1. #include <iostream>
  2. class A{
  3. public:
  4. A(int a, int &b):i1(a),i2(b){};
  5. public:
  6. int i1;
  7. int &i2;
  8. };
  9. int main()
  10. {
  11. int t  = 60;
  12. A a(45,t);
  13. std::cout << a.i1 << " " << a.i2 << std::endl;
  14. return 0;
  15. }
 
 

C++中 int i 与 int &i 注意事项的更多相关文章

  1. C#中(int)、int.Parse()、int.TryParse()和Convert.ToInt32()的区别

    转自:http://www.cnblogs.com/leolis/p/3968943.html 在编程过程中,数据转换是经常要用到的,C#中数据转换的方法很多,拿将目标对象转换为 整型(int)来讲, ...

  2. C#中(int)、int.Parse()、int.TryParse()和Convert.ToInt32()的区别 <转>

    作者:Statmoon 出处:http://leolis.cnblogs.com/   在编程过程中,数据转换是经常要用到的,C#中数据转换的方法很多,拿将目标对象转换为整型(int)来讲,有四种方法 ...

  3. MySql中的tinying,smallint,int,bigint的类型介绍——转载

    tinyint 从 0 到 255 的整型数据.存储大小为 1 字节. smallint 从 -2^15 (-32,768) 到 2^15 – 1 (32,767) 的整型数据.存储大小为 2 个字节 ...

  4. C#/C++ 中字节数组与int类型转换

    1.C#中int和byte[]转换: /// <summary> /// 把int32类型的数据转存到4个字节的byte数组中 /// </summary> /// <p ...

  5. 嵌入式中的 *(volatile unsigned int *)0x500 解释

    C语言中*(volatile unsigned int *)0x500的解释: 如下: (unsigned int *)0x500:将地址0x500强制转化为int型指针*(unsigned int ...

  6. java中字符串String 转 int(转)

    java中字符串String 转 int String -> int s="12345"; int i; 第一种方法:i=Integer.parseInt(s); 第二种方法 ...

  7. MVC Action,Service中筛选int 和list<int>

    action: public ActionResult DeleteByID(int id) { this.MessageService.DeleteMailTemplate(id); var fro ...

  8. c++ 中关于int,unsigned int , short的关系与应用

    转载:http://www.cppblog.com/xyjzsh/archive/2010/10/20/130554.aspx?opt=admin   int类型比较特殊,具体的字节数同机器字长和编译 ...

  9. [转载] java中byte数组与int,long,short间的转换

    文章转载自http://blog.csdn.net/leetcworks/article/details/7390731 package com.util; /** * * <ul> * ...

  10. 【转】java中byte数组与int类型的转换(两种方式)----不错

    原文网址:http://blog.csdn.net/piaojun_pj/article/details/5903009 java中byte数组与int类型的转换,在网络编程中这个算法是最基本的算法, ...

随机推荐

  1. WGS84投影的WKID说明

    关于WKID的几点说明 1.ArcGIS Server 10中: EPSG 3857         WGS_1984_Web_Mercator_Auxiliary_Sphere ESRI 10211 ...

  2. 0000 - Spring Cloud 概述

    1.概述 Spring Cloud是一系列框架的有序集合,它利用Spring Boot的开发便利性简化了分布式系统的开发,比如服务发现.服务网关.服务路由.链路追踪等.Spring Cloud并不重复 ...

  3. Try Catch Finally总结

    Try Catch Finally探究 1. try.catch.finally语句中,在如果try语句有return语句,则返回的是当前try中变量此时对应的值,此后对变量做任何的修改,都不影响tr ...

  4. Windows配置多个git用户

    Window配置多个Git账户,SSH连接GitHub.GitLab 最新版本GIt配置对应多个Git仓库(不需要添加多个用户名和邮箱): 在本地git上添加一个用户名和邮箱,生成一对公钥和私钥,把公 ...

  5. ip route rule 路由策略 高级路由 捆绑 网桥

    http://lwfs.net/2005/11/28/10/ #!/bin/bash IP0= IP1= GW0= GW1= NET0= NET1= DEV0=eth0 DEV1=eth1 # com ...

  6. Windows下sbt安装配置

    1.下载sbt1.2.8 官网:https://sbt-downloads.cdnedge.bluemix.net/releases/v1.2.8/sbt-1.2.8.tgz 2.配置环境 #SBT_ ...

  7. Dataset,DataTable

    public DataTable test(int id) { var dt = GetLanguageDataTable(pfAppId); dt.AsEnumerable().ToList().F ...

  8. python数据类型总结

    按存值个数区分 标量/原子类型 数字,字符串 容器类型 列表,元组,字典 按可变不可变区分 可变 列表,字典 不可变 数字,字符串,元组 按访问顺序区分 直接访问 数字 顺序访问(序列类型) 字符串, ...

  9. cocos设置 相机矩阵和投影矩阵 源码浅析

    在cocos中,最后设置视口大小,相机矩阵,裁剪矩阵是在setProjection方法中,源码如下: void Director::setProjection(Projection projectio ...

  10. django中路由系统和视图的对应关系(值的传递)-->主要内容(位置参数、关键字参数、额外参数、include分组[urls的分发]、命名分组、反向解析、APPEND_SLASH)

    路由系统也就是 urls.py文件,视图就是 views.py文件 路由系统里面要注意的事项 urlpatterns中的元素按照书写顺序从上往下逐一匹配正则表达式,一旦匹配成功则不再继续. 若要从UR ...