简体   繁体   English

int 函数参数工作不正常

[英]int function parameter not working properly

I'm trying to make something in C++ and I have a problem.我正在尝试用 C++ 做一些事情,但我遇到了问题。 I have this code:我有这个代码:

#include <iostream>
#include <string>
//---MAIN---
using namespace std;
int af1 = 1;
int af2 = 1;

void lettersort(int cnt1) {
    cout << "RESULT:" << cnt1 << endl;
    cnt1++;
    cout << "RESULT WHEN+:" << cnt1 << endl;
    cout << "RESULT IN GLOBAL INT:" << af2 << endl;
}

int main()
{
    lettersort(af2);
    return 0;
}

So is there any way so that cnt1++ affects af2 too, to make it bigger ?那么有没有办法让cnt1++影响af2 ,让它变大? I don't want to use af2++ directly because I want to sometimes use af1 .我不想直接使用af2++ ,因为我有时想使用af1

At the moment you are just passing af2 to cnt1 by value , so any changes to cnt1 are strictly local to the function lettersort .目前,您只是按值af2传递给cnt1 ,因此对cnt1任何更改都严格lettersort函数lettersort本地。 In order to get the behaviour you want you need to pass your cnt1 parameter by reference .为了获得您想要的行为,您需要通过引用传递您的cnt1参数。 Change:改变:

void lettersort(int cnt1)

to:到:

void lettersort(int &cnt1)

You are passing the argument by value .您正在按值传递参数。 Ie, you are copying the value of af1 to a local variable in lettersort .即,您正在将af1的值复制到lettersort的局部变量。 This integer is then incremented, and disposed of when the function ends, without affecting the original af1 .然后这个整数被递增,并在函数结束时被处理掉,而不影响原来的af1 If you want the function to be able to affect af1 , you should pass the argument by reference :如果您希望函数能够影响af1 ,您应该通过引用传递参数:

void lettersort(int& cnt1) { // Note the "&"

if i understood your question:如果我理解你的问题:

there are 2 ways you can do that.有两种方法可以做到这一点。

  1. make lettersort function return the new value, and put it in af2使lettersort函数返回新值,并将其放入 af2

     int lettersort(int cnt1) { cout << "RESULT:" << cnt1 << endl; cnt1++; cout << "RESULT WHEN+:" << cnt1 << endl; cout << "RESULT IN GLOBAL INT:" << af2 << endl; return cnt1; } int main() { af2 = lettersort(af2); return 0; }
  2. pass the value by reference.通过引用传递值。 you can read about it here , but generally its about passing a pointer to that value.你可以在这里阅读它,但通常它是关于传递一个指向该值的指针。 meaning whatever you do on the argument you are passing, will happen on the original var.这意味着无论你在传递的参数上做什么,都会发生在原始变量上。

example:例子:

    void foo(int &y) // y is now a reference
    {
        using namespace std;
        cout << "y = " << y << endl;
        y = 6;
        cout << "y = " << y << endl;
    } // y is destroyed here

    int main()
    {
        int x = 5;
        cout << "x = " << x << endl;
        foo(x);
        cout << "x = " << x << endl;
        return 0;
    }
  • here you have to just modified the argument pass to lettersort function as passed by reference.在这里,您只需将参数传递给按引用传递的 lettersort 函数进行修改。
  • for example if you declare and initialize any variable like:例如,如果您声明并初始化任何变量,如:

     int a=10; int &b = a;
  • now a and b refer to the same value.if you change a then the changes also reflect in b also.现在 a 和 b 指的是相同的值。如果您更改 a,则更改也会反映在 b 中。

  • so,所以,

     cout << a; cout << b;
  • both statement produce the same result across the program.这两个语句在整个程序中产生相同的结果。 so using this concept i modified the function argument and made it as by reference.所以使用这个概念我修改了函数参数并将其作为引用。

  • your correct code is :您的正确代码是:

     #include <iostream> #include <string> using namespace std; int af1 = 1; int af2 = 1; void lettersort(int &cnt1) { cout << "RESULT:" << cnt1 << endl; cnt1++; cout << "RESULT WHEN+:" << cnt1 << endl; cout << "RESULT IN GLOBAL INT:" << af2 << endl; } int main() { lettersort(af2); return 0; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM