简体   繁体   中英

Error binding reference to array passed to function (c++)

I'm using MinGW64 with eclipse , language is C++ as stated above.

I have the following code:

double * my_Function (my_Class I1, my_Class I2, double return_vector[3])
{
    double test[3];
    double (&rtest)[3]=test;
    double (&description_vector)[3] = return_vector;

    // some more code
    return (description_vector);
}

Binding rtest to test works fine, here the compiler tells me warning: unused variable , that is to be expected, as it's not used anywhere in the code I just wanted to find out if it works in principle.

However binding description_vector to return_vector results in the following error:

error: invalid initialization of reference of type 'double (&)[3]' from expression of type 'double*'

Why? Why is binding rtest to test legal but not the binding of description_vector to return_vector ?

So you may ask "But why binding the reference description_vector to return_vector ? Simply use return_vector in your return statement - it's the same after all."

I want to confer information to the reader of the code (basically me when I'll look at it in the future). This way you see that you have to pass a vector to the function for the purpose of returning it's calculation. You see that description_vector is an alias for return_vector and by the name of description_vector you can see what it is supposed to hold.

When you pass an array to a function it decays to a pointer to the first element and you can't bind it to a reference to an array. You should either pass a reference to an array as a parameter or make your life easier by using vectors (and iterators).

Even though the two array signatures look alike, the compiler will actually pass the return_array as a double* unlike the test array which is a double[3]. Basically they are different types. Passing arrays like this is shorthand for double*.

double * my_Function (my_Class I1, my_Class I2, double return_vector[3])
//                                              ^^^^^^^^^^^^^^^^^^^^^^^

The underlined syntax used in a function declaration means the same as double* . The bound is happily ignored by the compiler and is hardly more than a comment telling, say, the size the array that return_vector is supposed to point at.

Arrays are not pointers, they decay to pointers when passed to functions. This is the reason binding test works, as it is a proper array.

If you want to stick with references inside the function, you'll need to take a reference as a parameter, too.

Your function is actually identical to:

double * my_Function (my_Class I1, my_Class I2, double *return_vector)

This is an historical quirk of the syntax for function formal parameter lists.

Also, arrays cannot be passed by value in C++ (there are no array-type rvalues).

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