Java与C++函数参数传递比较——C++学习笔记一

原创 2006年05月20日 23:34:00

学好C++,为祖国四化做贡献。

简言之:Java都是值传递(pass-by-value),而C++中包括值传递(pass-by-value)和引用传递(pass-by-reference)。

先说Java,先做几点说明:

在Java中,无非就是两种类型,即基本类型和从Object继承下来的对象类型,而对象类型又包括String这种一旦初始化就不可改变内容的类型和BufferString这种可以初始化后可

以改变内容的类型。

然后看一下代码示例:

 package test;

public class Test {
 public static void main(String args[]) {
  Integer interger1, interger2;
  int i, j;
  interger1 = new Integer(10);
  interger2 = new Integer(50);
  i = 5;
  j = 9;
  System.out.println("Before Swap, Interger1 is " + interger1);
  System.out.println("Before Swap, Interger2 is " + interger2);
  swap(interger1, interger2);
  System.out.println("After Swap Interger1 is " + interger1);
  System.out.println("After Swap Interger2 is " + interger2);
  System.out.println("Before Swap i is " + i);
  System.out.println("Before Swap j is " + j);
  swap(i, j);
  System.out.println("After Swap i is " + i);
  System.out.println("After Swap j is " + j);

  StringBuffer sb = new StringBuffer("I am StringBuffer");
  System.out.println("Before change, sb is <" + sb + ">");
  change(sb);
  System.out.println("After change sb is <" + sb + ">");
 }

 public static void swap(Integer ia, Integer ib) {
  Integer temp = ia;
  ia = ib;
  ib = temp;
 }

 public static void swap(int li, int lj) {
  int temp = li;
  li = lj;
  lj = temp;
 }

 public static void change(StringBuffer ia) {
  ia.append(", but my content can be changed");
  //ia = new StringBuffer(",but my content can be changed");
 }
}

输出:

Before Swap, Interger1 is 10
Before Swap, Interger2 is 50
After Swap Interger1 is 10
After Swap Interger2 is 50
Before Swap i is 5
Before Swap j is 9
After Swap i is 5
After Swap j is 9
Before change, sb is <I am StringBuffer>
After change sb is <I am StringBuffer, but my content can be changed>

这很好解释,对于基本类型诸如int,传递进去的是存放int值的“内存单元”的一个copy,所以函数swap里面的int和外面的int根本就不是一个东西,当然不能反射出去影响外面

的int。而对于对象类型,我们同样可以这样认为,传递进去的是存放对象类型的指针的“内存单元”一个copy(虽然Java里面没有指针的概念,但这并不妨碍我们理解)。这样,

在swap函数里面,对其指针本身的值做任何操作当然不会影响外面的Integer,因为interger1和interger2的“内存单元”里面的值是不变的,其指向的对象类型也是没有变的。

然后这里需要说明一个问题,就是StringBuffer这种类型的对象了。因为其内容是可以改变的,所以change函数里面的“指针”通过类似“*”的操作,改变了StringBuffer对象的

本身,就显而易见了。(StringBuffer对象本身只有一个副本)

然后说C++了,里面的基本类型的诸如int的值传递大家都了然于胸,就不在这里废话了。然后另一种值传递可以称为指针引用传递(pass-by-value argument of pointer)(这个类

似上文说的Java中的对象类型的值传递),可以通过*操作,改变指针指向的值。示例程序如下,一看便知:

#include<iostream.h>

int main(){
 void test(int*, const char*);
 int i = 1;
 int* iptr = &i;
 cout<<"Before pass-by-value:"<<"/n/n";
 cout<<"i = "<<i<<", It's value of i"<<endl;
 cout<<"&i = "<<&i<<", It's address of i and value of iptr"<<endl;
 cout<<"*iptr = "<<*iptr<<", It's value of i"<<endl;
 cout<<"iptr = "<<iptr<<", It's value of iptr and address of i"<<endl;
 cout<<"&iptr = "<<&iptr<<", It's address of iptr-self"<<"/n/n";
 
 test(iptr, "pass-by-iptr");

 test(&i, "pass-by-&i");

 return 0;
}

void test(int* iiptr, const char* string){
 cout<<"When pass-by-value and :"<<"/n/n";
 cout<<"*iiptr = "<<*iiptr<<", It's value of i"<<endl;
 cout<<"iiptr = "<<iiptr<<", It's value of iiptr and address of i"<<endl;
 cout<<"&iiptr = "<<&iiptr<<", It's address of iiptr-self, different with iptr!"<<"/n/n";
}

输出:

Before pass-by-value:

i = 1, It's value of i
&i = 0x0012FF7C, It's address of i and value of iptr
*iptr = 1, It's value of i
iptr = 0x0012FF7C, It's value of iptr and address of i
&iptr = 0x0012FF78, It's address of iptr-self

When pass-by-value and :

*iiptr = 1, It's value of i
iiptr = 0x0012FF7C, It's value of iiptr and address of i
&iiptr = 0x0012FF24, It's address of iiptr-self, different with iptr!

When pass-by-value and :

*iiptr = 1, It's value of i
iiptr = 0x0012FF7C, It's value of iiptr and address of i
&iiptr = 0x0012FF24, It's address of iiptr-self, different with iptr!

在C++里面的第二种就是引用传递了(pass-by-reference)。见如下示例:

#include<iostream.h>
int main(){
 void test(int&, const char*);
 int i = 1;
 int &iref = i;
 cout<<"Before pass-by-reference:"<<"/n/n";
 cout<<"i = "<<i<<", It's value of i"<<endl;
 cout<<"&i = "<<&i<<", It's address of i and value of iptr"<<endl;
 cout<<"iref = "<<iref<<", It's value of iref and value of i"<<endl;
 cout<<"&iref = "<<&iref<<", It's address of iref-self, the same as i!"<<"/n/n";
 
 test(iref, "pass-by-iref");

 test(i, "pass-by-i");

 return 0;
}

void test(int &iiref, const char* string){
 cout<<"When pass-by-reference and "<<string<<"/n/n";
 cout<<"iiref = "<<iiref<<", It's value of iiref and value of i"<<endl;
 cout<<"&iiref = "<<&iiref<<", It's address of iiref-self, the same as i!"<<"/n/n";
}

输出:

Before pass-by-reference:

i = 1, It's value of i
&i = 0x0012FF7C, It's address of i and value of iptr
iref = 1, It's value of iref and value of i
&iref = 0x0012FF7C, It's address of iref-self, the same as i!

When pass-by-reference and pass-by-iref

iiref = 1, It's value of iiref and value of i
&iiref = 0x0012FF7C, It's address of iiref-self, the same as i!

When pass-by-reference and pass-by-i

iiref = 1, It's value of iiref and value of i
&iiref = 0x0012FF7C, It's address of iiref-self, the same as i!

这里的引用(reference)说的明白一些,就是被传递参数的一个别名,或者更直接的理解就是被传递参数自己了,只是名字不同而已。那么既然自己都被pass过去了,那当然可以在function里面为所欲为了。赫赫。

renki_z对本文亦有贡献

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

C++学习笔记12——函数的参数传递

1,可行与不可行的参数传递情况: 2,举例: 假如有如下定义的函数 void fun_val(int i, const int ci); void fun_pointer(int *...

python学习笔记-(13)python中的函数参数传递

在之前的python学习笔记系列博客中,我已经描述过关于python中最简单的函数参数传递,根据参数位置传递。这里就不再赘述了,若想了解位置传递,请链接http://blog.csdn.net/u01...

[学习笔记]二位数组作为函数参数传递剖析

原文地址:http://www.cnblogs.com/wuyuegb2312/archive/2013/06/14/3135277.html 前言   很多文章不外乎告诉你下面这几种标准的形式...

c++函数参数传递效率比较,初始化的全局变量方法果然是最快的,而堆方式最慢.

int   b2=0;//全局初始化区  int  b3;  // 全局未初始化区void test(int* b){//传指针 500ms-520;如果是在堆区530-700ms (*b)++...

第二周——项目21C++语言中函数参数传递的三种方式

/*  * Copyright (c++) 2015 烟台大学计算机学院  * All right reserved.  * 文件名称:test.cpp  * 作    者: 商文轲  * 完成日期:...

C++函数参数传递的一大利器——引用(左值)

前言: 本文只浅显地介绍了三种函数参数传递的方式及其优劣,主要目的是为了介绍引用。在另一篇博文中将会用本文中的主要代码反汇编为汇编代码,从编译器处理函数调用的层面更深入地探讨三种函数参数传递的区别。...

第二周项目1——c/c++语言中函数参数传递的三种方式

问题及代码:/* *Copyright(c++)2015,烟台大学计算机与控制工程学院 *All rights reserved. *文件名称:cpp1.cpp 作者:宋 晨 完成日期:20...

Java和C++在函数参数传递上的不同

故事的起源起源于在leetcode上面刷题,刷到第39题Combination Sum。 Combination Sum 这道题是一道经典的回溯问题,一个被票选为最佳答案的解决方案如下:publi...

[c++学习笔记]反汇编角度看变量名和引用作为函数参数

1:默认情况下即以变量名作为函数参数时进行值传递 从反汇编中可以看出以下几点: 1:默认情况下进行的是值传递(mov) 2: 函数调用中参数的传递是从右向左依次传递的 2:以引用传...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)