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 size size / size move on the parameter and get element count). The compiler will also check the type to ensure that the array logic type is exactly the same as the array parameter type, ie if the parameter is declared as an array of 10 ints, then an array of logic 10 is ints and some more No.

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 run-time size, other versions (when array types are allowed to decay by pointer type) are reserved for situations. For example, the correct way to pass an array of the size of the compilation-time function in the function

  void foo (int (and arr) [10]); // Context of an array  

or

  zero foo (int (* arr) [10]);  

One must definitely use the "decay" approach incorrectly

  void foo (int arr []); // Pointer for an element / poor practice !!!  

The "decay" approach should generally be reserved for run-time size arrays and normally in different parameters with the actual size of the array

 < 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

Popular posts from this blog

sql - dynamically varied number of conditions in the 'where' statement using LINQ -

asp.net mvc - Dynamically Generated Ajax.BeginForm -

Debug on symbian -