I would like to be able to pass a const array argument to a method in C++.
I know that when you pass an array to method it is the same than passing a pointer to the first item of the array so an easy way is to use the pointer.
void myMethod(int * const inTab)
But having an array is sometimes better, you can write the size of the array for instance.
To pass an entire array to a function, only the name of the array is passed as an argument. result = calculateSum(num); However, notice the use of [] in the function definition. This informs the compiler that you are passing a one-dimensional array to the function.
Arrays can be passed to other methods just like how you pass primitive data type's arguments. To pass an array as an argument to a method, you just have to pass the name of the array without square brackets. The method prototype should match to accept the argument of the array type.
When we pass an array to a method as an argument, actually the address of the array in the memory is passed (reference). Therefore, any changes to this array in the method will affect the array.
Just like normal variables, Arrays can also be passed to a function as an argument, but in C/C++ whenever we pass an array as a function argument then it is always treated as a pointer by a function.
You can use a template taking the array size: http://ideone.com/0Qhra
template <size_t N>
void myMethod ( const int (& intArray) [N] )
{
std::cout << "Array of " << N << " ints\n";
return;
}
EDIT: A possible way to avoid code bloat would be to have a function that takes a pointer and a size that does the actual work:
void myMethodImpl ( const int * intArray, size_t n );
and a trivial template that calls it, that will easily be inlined.
template <size_t N>
void myMethod ( const int (& intArray) [N] )
{ myMethodImpl ( intArray, N ); }
Of course, you'ld have to find a way to test that this is always inlined away, but you do get the safety and ease of use. Even in the cases it is not, you get the benefits for relatively small cost.
Per 3.9.3:2
Any cv-qualifiers applied to an array type affect the array element type, not the array type (8.3.4).
and 8.3.4:1
Any type of the form “cv-qualifier-seq array of N T” is adjusted to “array of N cv-qualifier-seq T”, and similarly for “array of unknown bound of T”.
Also, per 8.3.5:5
After determining the type of each parameter, any parameter of type “array of T” or “function returning T” is adjusted to be “pointer to T” or “pointer to function returning T,” respectively.
That means that within a function taking an array parameter, the parameter type is actually a pointer, and because of 3.9.3:2 the pointer is non-cv-qualified:
void foo(const int parameter[10]) {
parameter = nullptr; // this compiles!
}
This does not affect the type of the function itself, because of another clause in 8.3.5:5
After producing the list of parameter types, any top-level cv-qualifiers modifying a parameter type are deleted when forming the function type.
Thus if you want to be able to pass an array with cv qualifiers, it must be by reference:
void foo(const int (¶meter)[10]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With