是什么样的一个引用到阵列参数有用吗? [英] What is useful about a reference-to-array parameter?

查看:100
本文介绍了是什么样的一个引用到阵列参数有用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我发现了一些code是这样的:

I recently found some code like this:

typedef int TenInts[10];
void foo(TenInts &arr);

您可以在体内做些什么富()这是非常有用的,你不能做,如果声明是:

What can you do in the body of foo() that is useful, that you could not do if the declaration was:

void foo(int *arr);    // or,
void foo(int arr[]);   // or,
void foo(int arr[10]); // ?

我发现,询问<一个问题href=\"http://stackoverflow.com/questions/404232/how-do-i-pass-a-reference-to-a-two-dimensional-array-to-a-function\">how传递到一个数组的引用。我猜我问的为什么

此外,只有<一href=\"http://stackoverflow.com/questions/2038229/when-is-pointer-to-array-useful/2042598#2042598\">one回答以当是指针数组有用吗?讨论函数的参数,所以我不认为这是一个重复的问题。

Also, only one answer to "When is pointer to array useful?" discussed function parameters, so I don't think this is a duplicate question.

推荐答案

的参考到阵列参数不允许数组类型衰减到指针类型。即确切的数组类型仍然是函数内部pserved $ P $。 (例如,您可以使用 sizeof的ARR / * sizeof的ARR 欺骗的参数,并得到了元素计数)。编译器也将为了确保数组参数类型是完全一样的数组参数类型,也就是说,如果该参数被声明为10个整数一个数组进行类型检查,参数必须是正好10数组整数而已。

The reference-to-array parameter does not allow array type to decay to pointer type. i.e. the exact array type remains preserved inside the function. (For example, you can use the sizeof arr / sizeof *arr trick on the parameter and get the element count). The compiler will also perform type checking in order to make sure the array argument type is exactly the same as the array parameter type, i.e. if the parameter is declared as a array of 10 ints, the argument is required to be an array of exactly 10 ints and nothing else.

事实上,在情况下,当数组大小是固定的的编译时间的,使用引用到阵列(或指针到阵列)参数声明可以pceived为$ P $小学,preferred的方式来传递一个数组。其他变体(当阵列类型被允许衰减到指针类型)被保留用于情况时,有必要通过阵列的运行时的尺寸。

In fact, in situations when the array size is fixed at compile-time, using a reference-to-array (or pointer-to-array) parameter declarations can be preceived as the primary, preferred way to pass an array. The other variant (when the array type is allowed to decay to pointer type) are reserved for situations when it is necessary to pass arrays of run-time size.

例如,要编译时大小的数组传递给函数的正确方法是

For example, the correct way to pass an array of compile-time size to a function is

void foo(int (&arr)[10]); // reference to an array

void foo(int (*arr)[10]); // pointer to an array

这是不正确无疑的方法是使用一个腐朽的做法

An arguably incorrect way would be to use a "decayed" approach

void foo(int arr[]); // pointer to an element
// Bad practice!!!

在腐朽的方法通常应保留运行时大小的数组,并在一个单独的参数通常伴有阵的实际大小

The "decayed" approach should be normally reserved for arrays of run-time size and is normally accompanied by the actual size of the array in a separate parameter

void foo(int arr[], unsigned n); // pointer to an element
// Passing a run-time sized array

在换句话说,真的没有为什么的问题,当谈到引用到阵列(或指针到阵列)的传球。你应该自然地使用这种方法,默认情况下,只要你能,如果数组大小是固定在编译时。当您使用数组传递腐朽的方法应该真正出现了为什么的问题。的衰减的方法是只应该被用作专门特技用于使运行时的尺寸的阵列。

In other words, there's really no "why" question when it comes to reference-to-array (or pointer-to-array) passing. You are supposed to use this method naturally, by default, whenever you can, if the array size is fixed at compile-time. The "why" question should really arise when you use the "decayed" method of array passing. The "decayed" method is only supposed to be used as a specialized trick for passing arrays of run-time size.

上面基本上是一个比较通用的原理的直接后果。当你有型的重的对象 T ,你通常通过它或者通过指针 T * 引用或 T&安培; 。数组是没有从这个一般原则的例外。他们没有理由。

The above is basically a direct consequence of a more generic principle. When you have a "heavy" object of type T, you normally pass it either by pointer T * or by reference T &. Arrays are no exception from this general principle. They have no reason to be.

请尽管这在实践中往往是有意义的编写与运行时的大小的数组工作,特别是当它涉及到通用,库级函数的函数。这些功能将更加全面。这意味着,通常有一个很好的理由使用在现实生活中的腐朽的方式code,然而,这并没有当数组大小在编译时已知承认情况借口code的作者并相应地使用参考到阵列方法

Keep in mind though that in practice it is often makes sense to write functions that work with arrays of run-time size, especially when it comes to generic, library-level functions. Such functions are more versatile. That means that often there's a good reason to use the "decayed" approach in real life code, Nevertheless, this does not excuse the author of the code from recognizing the situations when the array size is known at compile time and using the reference-to-array method accordingly.

这篇关于是什么样的一个引用到阵列参数有用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆