简体   繁体   中英

C++ Passing array into function

I'm relatively new to C++ and am having a tough time passing my array into a separate function. Apologies for re-asking a question that has no doubt been answered a dozen times before, but I couldn't find any questions similar to the problem I have with my code.

int main()
{
    Array<int> intarray(10);
    int grow_size = 0;

    intarray[0] = 42;
    intarray[1] = 12;
    intarray[9] = 88;

    intarray.Resize(intarray.Size()+2);
    intarray.Insert(10, 6);

    addToArray(intarray);

    int i = intarray[0];

    for (i=0;i<intarray.Size();i++) 
    cout<<i<<'\t'<<intarray[i]<<endl;

    Sleep(5000);
}

void addToArray(Array<int> intarray)
{
    int newValue;
    int newIndex;

    cout<<"What do you want to add to the array?"<<endl;
    cin >> newValue;
    cout<<"At what point should this value be added?"<<endl;
    cin >> newIndex;

    intarray.Insert(newValue, newIndex);
}

You are passing a copy of the array, so any changes will not affect the original. Pass by reference instead:

void addToArray(Array<int> &intarray)
//                         ^

This is a special case of a more general question on parameters passing .

You may want to consider the following guidelines:

  1. If you want to pass something to a function to modify it inside the function (and make the changes visible to the caller), pass by reference ( & ).

    eg

     // 'a' and 'b' are modified inside function's body, // and the modifications should be visible to the caller. // // ---> Pass 'a' and 'b' by reference (&) // void Swap(int& a, int& b) { int temp = a; a = b; b = temp; } 
  2. If you want to pass something that is cheap to copy (eg an int , a double , etc.) to a function to observe it inside the function, you can simply pass by value .

    eg

     // 'side' is an input parameter, "observed" by the function. // Moreover, it's cheap to copy, so pass by value. // inline double AreaOfSquare(double side) { return side*side; } 
  3. If you want to pass something that is not cheap to copy (eg a std::string , std::vector , etc.) to a function to observe it inside the function (without modifying it), you can pass by const reference ( const & ).

    eg

     // 'data' is an input parameter, "observed" by the function. // It is in general not cheap to copy (the vector can store // hundreds or thousands of values), so pass by const reference. // double AverageOfValues(const std::vector<double> & data) { if (data.empty()) throw std::invalid_argument("Data vector is empty."); double sum = data[0]; for (size_t i = 1; i < data.size(); ++i) sum += data[i]; return sum / data.size(); } 
  4. In modern C++11/14 there is also an additional rule (related to move semantics ): if you want to pass something that is cheap to move and make a local copy of it, then pass by value and std::move from the value .

    eg

     // 'std::vector' is cheap to move, and the function needs a local copy of it. // So: pass by value, and std::move from the value. // std::vector<double> Negate(std::vector<double> v) { std::vector<double> result( std::move(v) ); for (auto & x : result) x *= -1; return result; } 

Since in your addToArray() function you modify the Array<int> argument, and you want modifications visible to the caller , you can apply rule #1, and pass by reference ( & ):

void addToArray(Array<int> & intarray)

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