2

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.

2
  • ChangeX(vectors, 2); -- You're changing vectors, not v1 or v2. Print vectors[0]._x instead. Commented Jul 15, 2020 at 22:13
  • 2
    Unrelated: potential terminology problem Vect looks more like a point than a vector. Commented Jul 15, 2020 at 22:14

1 Answer 1

7

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.