引用传递和指针传递有何区别_什么是引用,它与指针有何不同?

引用传递和指针传递有何区别

This article consists of the description of both references [in C++] and pointers [preferred in C/C++]. The description would finally lead to a conclusion which will draw a difference line between pointers and reference. This article does not conclude that one of these should not be preferred. It will just simply provide you some basic overview over the topics.

本文由[在C ++中]指针[在C / C ++中首选 ]的描述组成。 该描述将最终得出结论,该结论将在指针和引用之间画出一条区别线。 本文并未得出结论,不应首选其中之一。 它只是为您提供有关这些主题的一些基本概述。

C ++中的参考 (References in C++)

A reference variable is basically nothing but an alias name or in simpler terms another name declared for a pre-existing variable. For the purpose of declaring a reference or an alias the address-of (&) is used. This operator placed before the variable in declaration statement. After a reference is declared then that variable can be called by either using the original variable name or the alias name given to that variable using & operator.

引用变量基本上只是别名,或更简单的说法是为现有变量声明的其他名称。 为了声明引用或别名,使用地址( & )。 该运算符放在声明语句中的变量之前。 声明引用后,可以使用原始变量名或使用&运算符为该变量指定的别名来调用该变量。

In simpler words, let a variable name say int a=10; be a variable declared. This variable is a label marking a memory location. Similarly, when we declared a reference say int &r=a; this is yet another label marking that same memory location so we can access that memory by calling any of these two variable names.

用简单的话来说,让变量名说int a = 10; 是声明的变量。 此变量是标记内存位置的标签。 同样,当我们声明引用时,请说int&r = a;。 这是另一个标记相同内存位置的标签,因此我们可以通过调用这两个变量名中的任何一个来访问该内存。

References are similar to pointers or in simpler way of understanding they are weak pointers basically developed for the purpose of is in functions as they act as formal parameters[variables declared inside the function declaration parenthesis] in various functions to support reference passing in functions. When a reference is passed into a function, the function works on the original copy of the variable instead of duplicate values as in value passing functions. This enables us to show the operations performed inside the function in the outside of the function. Also references are used for function arguments and return values.

引用类似于指针,或者以更简单的理解方式,它们是为函数目的而开发的弱指针,因为它们在各种函数中充当形式参数(在函数声明括号内声明的变量),以支持在函数中传递引用。 将引用传递给函数时,该函数将在变量的原始副本上工作,而不是像值传递函数中那样使用重复值。 这使我们能够在函数外部显示函数内部执行的操作。 引用也用于函数参数和返回值。

参考的C ++实现 (C++ implementation of References)

#include <iostream>
using namespace std;

int main()
{
	int i;
	double d;

	int &a=i;
	double &b=d;

	i=20;
	d=0.97;

	cout<<"Value of original  int variable i:"<<i<<endl;
	cout<<"Value of reference int variable a:"<<a<<endl;

	cout<<"Value of original  double variable d:"<<d<<endl;
	cout<<"Value of reference  double variable b:"<<b<<endl;
	
	return 0;
}

Output

输出量

    Value of original  int variable i:20
    Value of reference int variable a:20
    Value of original  double variable d:0.97
    Value of reference  double variable b:0.97

There are two basic concepts which a C++ programmer should have knowledge on the topic of references:

C ++程序员应该对引用主题有两个基本概念:

a)引用作为参数 (a) References as Parameters)

This is how a call by reference function works using references.

这是通过引用进行的调用功能使用引用进行工作的方式。

Example:

例:

#include <iostream>
using namespace std;

/* Here &a,&b are references 
which are used as formal paramater*/
void swap(int&a, int&b)  
{
	int t;
	t=a;
	a=b;
	b=t;
}

int main()
{
	//Here x,y are local variables
	int x=100;     
	int y=200;
	
	cout<<"Initial value of x:"<<x<<endl;
	cout<<"Initial value of y:"<<y<<endl;
	
	//Calling the function swap
	swap(x,y);     
	
	cout<<"Value of x after swapping:"<<x<<endl;
	cout<<"Value of y after swapping:"<<y<<endl;
	
	return 0;
}

Output

输出量

    Initial value of x:100
    Initial value of y:200
    Value of x after swapping:200
    Value of y after swapping:100

b)引用作为返回值 (b) References as Return Values)

A C++ program can also return a reference like it returns a pointer.

C ++程序还可以像返回指针一样返回引用。

Example

#include <iostream>
#include <ctime>
using namespace std;
double v[]={11.5,20.1,33.9,45.6,50.2};

double &setValue(int i)
{
	// Returns reference to variable i
	return v[i];      
}

int main()
{
	cout<<"Initial values of v:"<<endl;
	for(int i=0;i<5;i++)
	{
		cout<<"v["<<i<<"] =";
		cout<<v[i]<<endl;
	}

	setValue(2)=12.9;   //Changes 3rd value
	setValue(4)=6.7;    //Changes 5th value

	cout<<"v after change:"<<endl;
	for(int i=0;i<5;i++)
	{
		cout<<"v["<<i<<"] =";
		cout<<v[i]<<endl;
	}
	
	return 0;
	
}

Output

输出量

    Initial values of v:
    v[0] =11.5
    v[1] =20.1
    v[2] =33.9
    v[3] =45.6
    v[4] =50.2
    v after change:
    v[0] =11.5
    v[1] =20.1
    v[2] =12.9
    v[3] =45.6
    v[4] =6.7


翻译自: https://www.includehelp.com/cpp-tutorial/references-vs-pointers.aspx

引用传递和指针传递有何区别

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
传递引用传递指针传递C++ 中常用的参数传递方式,它们之间有以下区别: 1. 值传递:通过值传递方式,函数会创建参数的副本,并在函数内部使用该副本进行操作。对参数的修改不会影响原始值。值传递适用于参数不需要被修改的情况,可以保护原始数据的安全性。但是,对于大型对象或复杂数据结构,由于需要复制参数的副本,可能会带来性能开销。 2. 引用传递:通过引用传递方式,函数会直接操作原始值,而不是创建副本。对参数的修改会直接影响原始值。引用传递适用于需要修改参数,并且避免复制大型对象的情况。它提供了更高的性能,并且能够使函数内部的修改反映到原始数据上。 3. 指针传递:通过指针传递方式,函数接收一个指向原始值的指针。函数可以通过解引用指针来操作原始值,并且可以修改指针指向的内存。与引用类似,指针传递也可以用于修改参数,并且能够避免复制大型对象。但是,需要注意的是指针可能为空(nullptr),需要进行空指针检查。 需要注意的是,无论是引用传递还是指针传递,都可以用于修改参数的值。但是引用传递在语法上更简洁,而指针传递可以提供更多的灵活性(如空指针处理、重新指向其他对象等)。 综上所述,值传递引用传递指针传递在参数传递方式上有不同的特点和用途。在选择时,需要考虑参数的修改需求、性能开销和代码的可读性等因素。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值