简体   繁体   中英

Pass reference of array of objects to function

I'm trying to change a parameter of an object inside an array, but it seems like it's creating a new one when I pass it to the function.

I already saw similar questions and answers like this one , but it doesn't work for me, because I don't have a fixed array size in the final code.

I created a very short version of the code to show the problem.

#include <iostream>

using namespace std;

class Vect {
public:
    Vect(int x, int y)
    {
        _x = x;
        _y = y;
    }

    int _x;
    int _y;
};

void ChangeX(Vect tests[], int size)
{
    for (int i = 0; i < size; i++) {
        tests[i]._x = 39;
    }
}

int main()
{

    Vect v1 = Vect(1,2);
    Vect v2 = Vect(6,3);
    cout << "Initial X: ";
    cout << v1._x;
    cout << "\n";

    Vect vectors[2] = { v1, v2 };

    cout << "Final X: ";
    ChangeX(vectors, 2);
    cout << v1._x;

    return 0;
}

I expect the output to be:

Initial X: 1
Final X: 39

But in reality is:

Initial X: 1
Final X: 1

Also, using C++ vectors is not the solution for now. I'm running low on program memory usage and have a very small space for extra code.

Your issue has nothing to do with your function. It is updating the contents of the array correctly. There is no need to pass the array itself by reference.

The real problem is with the array itself . The statement Vect vectors[2] = {v1, v2}; makes copies of the v1 and v2 objects in the array. Your function is modifying the copies , and then afterwards you output values from the originals instead of the copies . So, your output does not change, since the function is not modifying the originals .

To accomplish what you are attempting, pass in an array of pointers instead, where the pointers are pointing at the original objects, not copies of them, eg:

#include <iostream>

class Vect {
    public:
        Vect(int x, int y){
            _x = x;
            _y = y;
        };
        int _x;
        int _y;
};

void ChangeX(Vect* tests[], int size){
    for(int i = 0; i < size; i++){
        tests[i]->_x = 39;
    }
}

int main()
{
    Vect v1(1,2);
    Vect v2(6,3);

    std::cout << "Initial X:\n";
    std::cout << v1._x << "\n";
    std::cout << v2._x << "\n";
    
    Vect* vectors[2] = {&v1, &v2};    
    ChangeX(vectors, 2);

    std::cout << "Final X:\n";
    std::cout << v1._x << "\n";
    std::cout << v2._x << "\n";

    return 0;
}

Live Demo

Otherwise, start out with an array to begin with, eg:

#include <iostream>

class Vect {
    public:
        Vect(int x, int y){
            _x = x;
            _y = y;
        };
        int _x;
        int _y;
};

void ChangeX(Vect tests[], int size){
    for(int i = 0; i < size; i++){
        tests[i]._x = 39;
    }
}

int main()
{
    Vect vectors[2] = {Vect(1,2), Vect(6,3)};

    std::cout << "Initial X:\n";
    std::cout << vectors[0]._x << "\n";
    std::cout << vectors[1]._x << "\n";
    
    ChangeX(vectors, 2);

    std::cout << "Final X:\n";
    std::cout << vectors[0]._x << "\n";
    std::cout << vectors[1]._x << "\n";

    return 0;
}

Live Demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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