C ++:Argument Passing“通过引用传递” [英] C++: Argument Passing "passed by reference"

查看:139
本文介绍了C ++:Argument Passing“通过引用传递”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我理解为与任何其他变量一样,参数的类型决定了参数与其参数之间的交互。我的问题是,什么是推理背后为什么你会引用参数vs为什么你不会?为什么有些函数参数引用而有些不是?

解决方案

通过引用传递的能力有两个原因: / p>


  1. 修改函数参数的值

  2. 为了避免出于性能原因复制对象

修改参数的示例

  void get5and6(int * f,int * s)//使用指针
{
* f = 5;
* s = 6;
}

这可以用作:

  int f = 0,s = 0; 
get5and6(& f,& s); // f& s现在将是5& 6

 code> void get5and6(int& f,int& s)//使用引用
{
f = 5;
s = 6;
}

这可以用作:

  int f = 0,s = 0; 
get5and6(f,s); // f& s现在将是5& 6

当我们通过引用传递时,我们传递变量的地址。



例如:

pre> void SaveGame(GameState& gameState)
{
gameState.update
gameState.saveToFile(save.sav);
}

GameState gs;
SaveGame(gs)

  void SaveGame(GameState * gameState)
{
gameState-> update
gameState-> saveToFile(save.sav);
}

GameState gs;
SaveGame(& gs);



由于只传递地址,所以变量的值这对于巨大的对象可能真的很大)不需要复制。所以通过引用传递提高了性能,特别是当:


  1. 传递给函数的对象是巨大的(我将使用指针变量,调用者知道函数可能修改变量的值)

  2. 该函数可以被调用多次(例如在循环中)

另外,阅读 const 引用。使用它时,不能在函数中修改参数。


I understand as with any other variable, the type of a parameter determines the interaction between the parameter and its argument. My question is that what is the reasoning behind why you would reference a parameter vs why you wouldn't? Why are some functions parameters reference and some are not? Having trouble understanding the advantages of doing so, could someone explain?

解决方案

The ability to pass by reference exists for two reasons:

  1. To modify the value of the function arguments
  2. To avoid make copies of an object for performance reasons

Example for modifying the argument

void get5and6(int *f, int *s)  // using pointers
{
    *f = 5;
    *s = 6;
}

this can be used as:

int f = 0, s = 0;
get5and6(&f,&s);     // f & s will now be 5 & 6

OR

void get5and6(int &f, int &s)  // using references
{
    f = 5;
    s = 6;
}

this can be used as:

int f = 0, s = 0;
get5and6(f,s);     // f & s will now be 5 & 6

When we pass by reference, we pass the address of the variable. Passing by reference is similar to passing a pointer - only the address is passed in both cases.

For eg:

void SaveGame(GameState& gameState)
{
    gameState.update();
    gameState.saveToFile("save.sav");
}

GameState gs;
SaveGame(gs)

OR

void SaveGame(GameState* gameState)
{
    gameState->update();
    gameState->saveToFile("save.sav");
}

GameState gs;
SaveGame(&gs);


Since only the address is being passed, the value of the variable (which could be really huge for huge objects) doesn't need to be copied. So passing by reference improves performance especially when:

  1. The object passed to the function is huge (I would use the pointer variant here so that the caller knows the function might modify the value of the variable)
  2. The function could be called many times (eg. in a loop)

Also, read on const references. When it's used, the argument cannot be modified in the function.

这篇关于C ++:Argument Passing“通过引用传递”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆