Parameter Transmission

在C++中,函数参数传递有3种方式,分别是按值传递、按地址传递和按引用传递。其测试代码如下:

 1 //Parameter Transmission in C++
 2 #include<iostream>
 3  using namespace std;
 4
 5  void swap_value(int a,int b){     //交换形参a、b的数值
 6
 7    cout<<"a="<<a<<" b="<<b<<endl;
 8    int temp=a;
 9        a=b;
10        b=temp;
11
12    cout<<"After the function is called:"<<endl;
13    cout<<"a="<<a<<" b="<<b<<endl;
14  }
15
16  void swap_reference(int &a,int &b){     //交换形参a、b的引用
17    cout<<"a="<<a<<" b="<<b<<endl;
18
19    int temp=a;
20        a=b;
21        b=temp;
22
23    cout<<"After the function is called:"<<endl;
24    cout<<"a="<<a<<" b="<<b<<endl;
25  }
26
27  void swap_pointer(int *a,int *b){     //交换形参a、b的地址
28    cout<<"a="<<*a<<" b="<<*b<<endl;
29
30    int  temp=*a;
31         *a=*b;
32         *b=temp;
33
34    cout<<"After the function is called:"<<endl;
35    cout<<"a="<<*a<<" b="<<*b<<endl;
36  }
37
38  void main(void){
39
40    int x1=100,y1=200;
41    cout<<"The result of passing by value is: "<<endl;
42    cout<<"Before the function is called:"<<endl;
43    cout<<"(x1="<<x1<<",y1="<<y1<<")\n";
44    swap_value(x1,y1);
45    cout<<"(x1="<<x1<<",y1="<<y1<<")\n\n";
46
47    int x2=100,y2=200;
48    cout<<"The result of passing by reference is: "<<endl;
49    cout<<"Before the function is called:"<<endl;
50    cout<<"(x2="<<x2<<",y2="<<y2<<")\n";
51    swap_reference(x2,y2);
52    cout<<"(x2="<<x2<<",y2="<<y2<<")\n\n";
53
54    int x3=100,y3=200;
55    cout<<"The result of passing by pointer is: "<<endl;
56    cout<<"Before the function is called:"<<endl;
57    cout<<"(x3="<<x3<<",y3="<<y3<<")\n";
58    swap_pointer(&x3,&y3);
59    cout<<"(x3="<<x3<<",y3="<<y3<<")\n";
60
61 }
62  //The screen display are following
63  /*The result of passing by value is:
64    Before the function is called:
65    (x1=100,y1=200)
66    a=100 b=200
67    After the function is called:
68    a=200 b=100
69    (x1=100,y1=200)
70
71    The result of passing by reference is:
72    Before the function is called:
73    (x2=100,y2=200)
74    a=100 b=200
75    After the function is called:
76    a=200 b=100
77    (x2=200,y2=100)
78
79    The result of passing by pointer is:
80    Before the function is called:
81    (x3=100,y3=200)
82    a=100 b=200
83    After the function is called:
84    a=200 b=100
85    (x3=200,y3=100)
86  */

简单解释一下,按值传递很容易理解,实参与形参分配到不同单元;不同单元内容交换,实参原值并没有发生改变。由于C++中有指针和引用这2种特殊的数据类型,故根据它们的定义我们可以知道,无论是按地址传递还是按引用传递,其本质是一样的,都是建立实参与形参地址上统一的基础之上的(即,对应实参的地址与形参的地址是一样的),从而可以实现改变实参内容。

在JAVA中,函数参数传递表面上有两种,实际上都将归于一种---按值传递。其测试代码如下:

 1 package test;
 2 //Parameter Transmission in JAVA
 3 public class Test_One {
 4     public static void testint(int xx){xx=10;}
 5     public static int returnint(int zz){zz=20;  return zz;}
 6     public static void testdouble(double yy){ yy=2.5;}
 7     public static void testString(String aa){aa="yang";}
 8
 9     public static class Object{ int temp=6;}
10     public static void testStringArray1(String [] cc){cc[0]="987456";}
11     public static void testStringArray2(String [] ee){
12         String [] a=new String [20];
13         a=ee;
14     }
15     public static void testObject(Object dd){dd.temp++;}
16
17     public static void main(String [] args){
18         int x=0;
19         System.out.println("x="+x);
20         testint(x);
21         System.out.println("x="+x);
22
23         int z=0;
24         System.out.println("z="+z);
25         z=returnint(z);
26         System.out.println("z="+z);
27
28         double y=5.4;
29         System.out.println("y="+y);
30         testdouble(y);
31         System.out.println("y="+y);
32
33         String name="wang";
34         System.out.println("name="+name);
35         testString(name);
36         System.out.println("name="+name);
37 //----------------------------------------------------------------
38         String [] sequence1={"123456"};
39         System.out.println("sequence1[0]="+sequence1[0]);
40         testStringArray1(sequence1);
41         System.out.println("sequence1[0]="+sequence1[0]);
42
43         String [] sequence2={"123456"};
44         System.out.println("sequence2[0]="+sequence2[0]);
45         testStringArray2(sequence2);
46         System.out.println("sequence2[0]="+sequence2[0]);
47
48         Object de=new Object();
49         System.out.println("de.temp="+de.temp);
50         testObject(de);
51         System.out.println("de.temp="+de.temp);
52         }
53 }
54 //The screen display are following
55 /*x=0
56   x=0
57   z=0
58   z=20
59   y=5.4
60   y=5.4
61   name=wang
62   name=wang
63   sequence1[0]=123456
64   sequence1[0]=987456
65   sequence2[0]=123456
66   sequence2[0]=123456
67   de.temp=6
68   de.temp=7
69  */

总结:0.表面上看,可得出如下结论:对于形参是基本数据类型的,函数参数传递是按值传递(即对形参的修改并不影响实参的结果);

对于形参是对象类型的, 函数参数传递是按引用传递(即对形参的修改直接影响实参的结果)。而实际上它们都是按值传递。

1.对于参数是基本数据类型,一般而言,形参到实参是单向的;我们若要实现形参与实参双向传递,可通过返回值方法来实现。(见上述的x和z)
            对于参数是对象类型,一般而言,形参到实参是双向的;我们若要实现单向,可通过在方法内部创建新对象,并且实参先传给形参,形参再传给
            新对象。

具体的解释请参考“Parameter passing in Java - by reference or by value?”里面写的非常详细。

下面给出其网页链接http://www.yoda.arachsys.com/java/passing.html

Parameter Transmission,布布扣,bubuko.com

时间: 2024-10-09 08:21:02

Parameter Transmission的相关文章

&lt;transmission&gt; element is used to link actuators to joints

For the purposes of gazebo_ros_control in its current implementation, the only important information in these transmission tags are: <joint name=""> - the name must correspond to a joint else where in your URDF <type> - the type of t

ORA-00119: invalid specification for system parameter LOCAL_LISTENER

重启oracle是提示错误ORA-00119: invalid specification for system parameter LOCAL_LISTENER. 解决方法: 命令查看错误信息:oerr ora 00119 00119, 00000, "invalid specification for system parameter %s" // *Cause: The syntax for the specified parameter is incorrect. // *Ac

ORA-16032: parameter LOG_ARCHIVE_DEST_3 destination string cannot be translated

standby down了,所以重启报错,ORA-16032: parameter LOG_ARCHIVE_DEST_3 destination string cannot be  translated :?1,重启报错信息SQL> startup                                                                                                                              

HibernateSystemException: ordinal parameter mismatch

使用Hibernate + MySQL数据库,写HQL语句时,不要忘了不支持MySQL的limit 来自为知笔记(Wiz)HibernateSystemException: ordinal parameter mismatch,码迷,mamicode.com

bash shell parameter expansion

1 ${parameter%word}和${parameter%%word} ${parameter%word},word是一个模式,从parameter这个参数的末尾往前开始匹配.单个%进行最短匹配,也就是说,从后往前匹配,遇到了匹配成功立马停止. ${parameter%%word},word是一个模式,从parameter这个参数的末尾往前开始匹配.双%进行最长匹配,也就是说,从后往前匹配,遇到了匹配继续匹配,一直匹配到开头,找到最长的匹配. path=/home/mem/long.fil

OpenWrt 中安装配置Transmission

参考文章https://wiki.openwrt.org/doc/uci/transmission 1. 安装包必装的 transmission-daemon-openssl (后台服务)选装的 transmission-cli-openssl (命令行管理) transmission-remote-openssl (远程UI管理) transmission-web-openssl (web管理)我选择的是 daemon 和 remote, 在编译OpenWrt的时候已经加入另外需要安装一个客户

spice model parameter

===================BSIM4.3.0 Model Selectors/Controllers============ LEVEL SPICE3 model selector VERSION Model version BINUNIT Binning unitr PARAMCHK Switch for parameter value check MOBMOD Mobility model RDSMOD Bias-dependent source/drain resistance

ORA-0019 invalid specification for system parameter LOCAL_LISTENER

SQL>startup ORA-00119:   invalid specification for system parameter LOCAL_LISTENER ORA-00130:   invalid listener address '(ADDRESS=(PROTOCOL=TCP) (HOST=bl660)(PORT=1521))' 数据库启动失败 解决办法: 在/etc/hosts里面加入主机名和IP即可

Attribute 和 Parameter 的区别

(1)HttpServletRequest类有setAttribute()方法,而没有setParameter()方法 (2)当两个Web组件之间为链接关系时,被链接的组件通过getParameter()方法来获得请求参数, (3)当两个Web组件之间为转发关系时,转发目标组件通过getAttribute()方法来和转发源组件共享request范围内的数据. 一般通过表单和链接传递的参数使用getParameter 通过request.setAttribute("name","