c++ - What is useful about a reference-to-array parameter? -
I recently found some code like this:
typedef int tenints [10 ]; Zero fu (ten inches and ar);
What you can do in the body of foo ()
which is useful, if you can not declare:
< Code> zero foo (int * arr); // or, zero foo (int arr []); // or, zero foo (ant ar [10]); //?
I got a question that asks, I think I'm asking why .
Besides, only "when indicator is useful?" Function parameters are discussed, so I do not think this is a duplicate question.
The reference-array parameter does not allow orange type to decay with pointer type, that is, the exact array type The function is preserved inside. (For example, you can use the In fact, when the size of the array is fixed at compile-time , reference-to-array (or pointer-to-array) parameter declarations will be assigned an array Can be considered as the primary, preferred way of passing. When required to pass the arrays of or One must definitely use the "decay" approach incorrectly The "decay" approach should generally be reserved for run-time size arrays and normally in different parameters with the actual size of the array size size / size
void foo (int (and arr) [10]); // Context of an array
zero foo (int (* arr) [10]);
void foo (int arr []); // Pointer for an element / poor practice !!!
< Code> Zero FU (Ent Ar [], unsigned N); // Passing an array of pointer // one run-time size for an element
In other words, there really is no "why" question when this reference-to-array (Or pointer-to-array) passing you want to use this method by default, by default, whenever you can, if the size of the array is fixed at compile-time "why" the question actually is generated When you use the "decay" method of regeneration of the array " If "method of use is considered only as a specific move to pass the runtime size of the array.
The above is basically a direct result of more general theory when you have "heavy" object of type "code" T , then you usually use pointer T *
Or reference T & amp; The array is no exception to this general principle, they have no reason.
Keep in mind that in practice it often makes sense that tasks that work with run-time size arrays, especially when it is normal, library-level functions. Such works are more versatile, this means that there is often a good reason to use the "decay" approach in real life code, however, it does not forgive the author of the code to recognize the circumstances when the size of the array is time Compilation and reference-reference -night method accordingly
Comments
Post a Comment