C++传引用数组
来源:互联网 发布:开票软件登录密码 编辑:程序博客网 时间:2024/05/16 09:44
Passing an Array to a Function
Last updated Jun 17, 2005.
In aprevious column, I claimed that passing an array as a function argument was impossible. The problem is that the array is implicitly converted, or decays, into a pointer. The pointer, alas, doesn't store the array's dimension; it doesn't even tell you that the variable in question is an array. I recently received an e-mail message from a reader who proposed a technique for passing arrays as arguments without the implicit conversion to pointers. Here it is:
"I'm not sure you are correct when you say in your article that functions in C++ can't receive array arguments. I have written functions that take array arguments before. It only works for statically sized arrays, and the parameter has to specify the size of the array, but it is possible. The syntax is something like:
void foo(int (&array)[10]);which declares a function that takes an array of 10 integers. This does not seem useful at first, but by using template parameter deduction you can write generic functions that operate on statically sized arrays in this way:
template <typename T, size_t size>void foo(T (&array)[size]){ //sort array, find the sum, min, max, etc}Of course as you say in your article, you have to be careful that the array does not decay to a pointer as you are passing it around. In this case though, the code should simply not compile since the template will not match."
Before I go on, let's parse the following function declaration, which might look a bit cryptic:
void foo(int (&array)[10]);
What's interesting here is how the parameter is declared: int (&array)[10] is a reference to an array of 10ints. So, what gets passed on the callee's stack is actually a reference, not a real array; though, for most practical purposes, there is no difference between the two. (For a review of the three argument passing mechanisms of C++, seethis section.)
Unlike passing an array by value, which causes the array to decay into a bare pointer, the use of a reference to a statically-allocated array is seemingly safer, because the callee has access to the type information associated with the array. Particularly, a sizeof() expression returns the correct array size even when used inside the callee:
void foo(int (&arr)[10]){ cout<<sizeof(arr)<<endl; //output: 40}int main(){ int arr[10]; foo(arr);}
When you use a reference to a statically sized array, the compiler is also capable of detecting type mismatches at the place of all:
int arr[5]; //wrong dimensionfoo(arr); //compiler diagnostic
There are a few caveats here, though. The main problem is that a reference to an array is a slippery type -- you can inadvertently convert it to a barepointer, thus losing important type information. Consider the following example:
void func(int* p) {}void foo(int (&arr)[10]){ func(arr);}
When func() is called, the argument arr decays into a pointer. Remember: once an array has decayed into a pointer, the array's type information is lost. This isn't the only problem that might arise, though. Consider the followingtypeid() expression. I expected it to produce a complete description of the array's type, including its dimension:
void foo(int (&arry)[10]){ cout<<typeid(arry).name()<<endl;}
However, the cout expression reports "int *". We can conclude that there are limits as to how far you can push this technique. If it isn't used in generic functions that operate on statically sized arrays, I still recommend thearray_wrapper class template presented in the original column as it offers a safer and efficient alternative.
- c/c++ 传引用,数组传引用 区别
- 数组引用(C++)
- c++-引用二维数组
- [C/C++]数组的引用、多维数组
- c++-引用一维数组
- 【C】指针、引用和数组
- C&C++关于数组的定义,引用传参问题
- [C++]关于数组的引用和引用的数组
- C++传引用数组
- 数组引用传参
- C中数组的另类引用
- C中数组的另类引用
- C#中数组是引用类型
- C语言之指针,数组,引用
- C/C++对数组的引用
- C/C++指针、数组和引用
- 【C++】指针,数组名,引用的区别
- C语言之指针,数组,引用
- Java中没有sizeof-------来自java编程思想
- C++ 学习笔记 <2> C++ IO
- 简单自定义拖动条
- 一篇关于多线程同步的好文章
- Android之SharedPreference
- C++传引用数组
- css用一张背景图background-position定位
- 从《愤怒的小鸟》看Google Play存在的问题
- 反射一个类中的所有方法
- 第1章 C:穿越时空的迷雾 下
- 9 Android WIFI 驱动移植
- zookeeper安装
- CMPXCHG8B - 比较并交换 8 字节
- 文本框里的内容垂直居中