学好C++,为祖国四化做贡献。
简言之:Java都是值传递(pass-by-value),而C++中包括值传递(pass-by-value)和引用传递(pass-by-reference)。
先说Java,先做几点说明:
在Java中,无非就是两种类型,即基本类型和从Object继承下来的对象类型,而对象类型又包括String这种一旦初始化就不可改变内容的类型和BufferString这种可以初始化后可
以改变内容的类型。
然后看一下代码示例:
java 代码
- 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");
-
- }
- }
-
输出:
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中的对象类型的值传递),可以通过*操作,改变指针指向的值。示例程序如下,一看便知:
cpp 代码
- #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)。见如下示例:
cpp 代码
- #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对本文亦有贡献
分享到:
相关推荐
函数调用,参数堆栈等学习资料收集 关于函数调用时堆栈的变化分析(转自Jim's blog) - H_S_的学习总结与心得 - 博客园....函数参数堆栈.txt 函数调用堆栈分析.doc 转贴:关于函数调用的深入分析 百度空间_应用平台.mht
Java SafeEngine API 函数的主要目的在于提供一个安全的数字证书系统,帮助开发者开发安全的数字证书应用程序。该API接口提供了丰富的功能,包括基本项获取、密钥库、证书、私钥和公钥等,帮助开发者快速开发安全的...
2. **函数接口**:详细列出函数库提供的所有函数,包括参数、返回值和功能描述,帮助开发者快速找到所需的函数。 3. **示例代码**:通过实例展示如何在实际项目中调用这些函数,让开发者能够直观地理解如何使用。 ...
本篇文章将围绕"Flex与Java进行CRUD操作"这一主题展开,详细介绍如何使用Flex作为前端,通过与Java后端交互来实现创建(Create)、读取(Retrieve)、更新(Update)和删除(Delete)数据的功能。 1. **Flex基础知识** ...
史上最全的android和java面试文档集。包括有: java程序员面试宝典.txt Java面试宝典2011版-1C,Java基础部分.doc... Java面试题及答案(基础题122道) - 在梦想与现实之间徘徊 - JavaEye技术网站.mht 等等,还有好多。
通过阅读"java面试题及答案(基础题122道,代码题19道).doc"和"java练_习_题.doc",可以针对性地复习和练习这些知识点,而"JAVA基础笔试.doc"和"中软的面试题(转贴).doc"则可能包含实际的面试题目,可以帮助模拟...
Eclipse是一款广泛使用的集成开发环境(IDE),尤其适合Java编程,而“客户端函数”通常指的是运行在用户设备上的代码,如Web浏览器中的JavaScript函数。 在Web应用程序开发中,客户端和服务器之间的交互是至关重要...
理解类的定义、构造函数、封装、继承和多态是Java编程的基础。同时,接口也是Java中实现多态性的重要方式。 3. **异常处理**:Java的异常处理机制是通过try-catch-finally语句块来实现的。理解如何正确抛出和捕获...
在本主题中,我们将深入探讨如何利用jQuery实现“转贴”功能,这是一种常见的社交媒体分享功能,允许用户将网页内容轻松分享到各种社交网络。 首先,让我们了解一下jQuery的核心概念。jQuery通过一种简洁的语法提供...
《易语言源码动网转贴》是一款基于易语言编程的源代码集合,主要用于实现网络论坛数据的迁移和转换功能。易语言是中国本土自主研发的一种高级编程语言,它以直观的汉字编程语法为特色,旨在降低编程难度,让更多人...
**Axis学习笔记(网页转贴)** Axis是一个开源的Java库,主要用于创建和使用Web服务。它是Apache软件基金会的一部分,广泛应用于开发基于SOAP(简单对象访问协议)的Web服务。本学习笔记将深入探讨Axis在Web服务开发...
由于现在流行的转贴工具都是基于浏览器的,转换速度比较慢,还得打开浏览器才能使用(同时受到浏览器版本限制)。 <br> 而这个小程序则完全不依赖于浏览器,以BFC采集器的UBB转换模块为基础,转换速度超快,...