C++ 当我更改函数内的参数时,调用者的参数是否也会更改?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1698660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:56:09  来源:igfitidea点击:

When I change a parameter inside a function, does it change for the caller, too?

c++

提问by Lisa

I have written a function below:

我在下面写了一个函数:

void trans(double x,double y,double theta,double m,double n)
{
    m=cos(theta)*x+sin(theta)*y;
    n=-sin(theta)*x+cos(theta)*y;
}

If I call them in the same file by

如果我在同一个文件中调用它们

trans(center_x,center_y,angle,xc,yc);

will the value of xcand ycchange? If not, what should I do?

会的价值xcyc变化?如果没有,我该怎么办?

回答by Mark Rushakoff

Since you're using C++, if you want xcand ycto change, you can use references:

由于您使用C ++,如果你想xcyc变化,您可以使用引用:

void trans(double x, double y, double theta, double& m, double& n)
{
    m=cos(theta)*x+sin(theta)*y;
    n=-sin(theta)*x+cos(theta)*y;
}

int main()
{
    // ... 
    // no special decoration required for xc and yc when using references
    trans(center_x, center_y, angle, xc, yc);
    // ...
}

Whereas if you were using C, you would have to pass explicit pointers or addresses, such as:

而如果您使用 C,则必须传递显式指针或地址,例如:

void trans(double x, double y, double theta, double* m, double* n)
{
    *m=cos(theta)*x+sin(theta)*y;
    *n=-sin(theta)*x+cos(theta)*y;
}

int main()
{
    /* ... */
    /* have to use an ampersand to explicitly pass address */
    trans(center_x, center_y, angle, &xc, &yc);
    /* ... */
}

I would recommend checking out the C++ FAQ Lite's entry on referencesfor some more information on how to use references properly.

我建议查看C++ FAQ Lite 关于参考的条目,以获取有关如何正确使用参考的更多信息。

回答by Chris Jester-Young

Passing by reference is indeed a correct answer, however, C++ sort-of allows multi-values returns using std::tupleand (for two values) std::pair:

通过引用传递确实是一个正确的答案,但是,C++ 排序允许使用std::tuple和(对于两个值)返回多值std::pair

#include <cmath>
#include <tuple>

using std::cos; using std::sin;
using std::make_tuple; using std::tuple;

tuple<double, double> trans(double x, double y, double theta)
{
    double m = cos(theta)*x + sin(theta)*y;
    double n = -sin(theta)*x + cos(theta)*y;
    return make_tuple(m, n);
}

This way, you don't have to use out-parameters at all.

这样,您根本不必使用输出参数。

On the caller side, you can use std::tieto unpack the tuple into other variables:

在调用方,您可以使用std::tie将元组解包为其他变量:

using std::tie;

double xc, yc;
tie(xc, yc) = trans(1, 1, M_PI);
// Use xc and yc from here on

Hope this helps!

希望这可以帮助!

回答by Soufiane Hassou

You need to pass your variables by reference which means

您需要通过引用传递变量,这意味着

void trans(double x,double y,double theta,double &m,double &n) { ... }

回答by timB33

as said above, you need to pass by reference to return altered values of 'm' and 'n', but... consider passing everything by reference and using const for the params you don't want to be altered inside your function i.e.

如上所述,您需要通过引用来返回“m”和“n”的更改值,但是...考虑通过引用传递所有内容并使用 const 作为您不想在函数中更改的参数,即

void trans(const double& x, const double& y,const double& theta, double& m,double& n)