C++ 通过引用传递数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10007986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ pass an array by reference
提问by kiriloff
is this allowed to pass an array by reference ?
这允许通过引用传递数组吗?
void foo(double& *bar)
Seems that my compiler says no. Why? What is the proper way to pass an array by reference? Or a work around? I have an array argument that my method should modify and that I should retrieve afterwards. Alternatively, I could make this array a class member, which works fine, but it has many drawbacks for other part of my code (that I would like to avoid).
似乎我的编译器说不。为什么?通过引用传递数组的正确方法是什么?还是变通?我有一个数组参数,我的方法应该修改它,然后我应该检索它。或者,我可以让这个数组成为一个类成员,这很好用,但它对我的代码的其他部分有很多缺点(我想避免)。
Thanks and regards.
感谢致敬。
回答by jrok
Arrays can only be passed by reference, actually:
数组只能通过引用传递,实际上:
void foo(double (&bar)[10])
{
}
This prevents you from doing things like:
这会阻止您执行以下操作:
double arr[20];
foo(arr); // won't compile
To be able to pass an arbitrary size array to foo
, make it a template and capture the size of the array at compile time:
为了能够将任意大小的数组传递给foo
,请将其设为模板并在编译时捕获数组的大小:
template<typename T, size_t N>
void foo(T (&bar)[N])
{
// use N here
}
You should seriously consider using std::vector
, or if you have a compiler that supports c++11, std::array
.
您应该认真考虑使用std::vector
,或者如果您有一个支持 c++11 的编译器,std::array
.
回答by James Kanze
Yes, but when argument matching for a reference, the implicit array to pointer isn't automatic, so you need something like:
是的,但是当参数匹配引用时,指向指针的隐式数组不是自动的,所以你需要类似的东西:
void foo( double (&array)[42] );
or
或者
void foo( double (&array)[] );
Be aware, however, that when matching, double [42]
and double []
are
distinct types. If you have an array of an unknown dimension, it will
match the second, but not the first, and if you have an array with 42
elements, it will match the first but not the second. (The latter is,
IMHO, very counter-intuitive.)
但是请注意,匹配时,double [42]
和double []
是不同的类型。如果您有一个未知维度的数组,它将匹配第二个而不是第一个,如果您有一个包含 42 个元素的数组,它将匹配第一个但不匹配第二个。(后者是,恕我直言,非常违反直觉。)
In the second case, you'll also have to pass the dimension, since there's no way to recover it once you're inside the function.
在第二种情况下,您还必须传递维度,因为一旦进入函数就无法恢复它。
回答by moooeeeep
As you are using C++, the obligatory suggestion that's still missing here, is to use std::vector<double>
.
当您使用 C++ 时,这里仍然缺少的强制性建议是使用std::vector<double>
.
You can easily pass it by reference:
您可以轻松地通过引用传递它:
void foo(std::vector<double>& bar) {}
And if you have C++11 support, also have a look at std::array
.
如果你有 C++11 支持,也看看std::array
.
For reference:
以供参考:
回答by Naszta
If you want to modify just the elements:
如果您只想修改元素:
void foo(double *bar);
is enough.
足够。
If you want to modify the address to (e.g.: realloc
), but it doesn't work for arrays:
如果要将地址修改为(例如:)realloc
,但它不适用于数组:
void foo(double *&bar);
is the correct form.
是正确的形式。
回答by Kirilodius
8.3.5.8 If the type of a parameter includes a type of the form “pointer to array of unknown bound of T” or “reference to array of unknown bound of T,” the program is ill-formed
8.3.5.8 如果参数的类型包括“指向 T 的未知边界数组的指针”或“对 T 的未知边界数组的引用”形式的类型,则程序格式错误
回答by parkovski
Like the other answer says, put the &
after the *
.
像其他回答说,放&
后*
。
This brings up an interesting point that can be confusing sometimes: types should be read from right to left. For example, this is (starting from the rightmost *
) a pointer to a constant pointer to an int.
这带来了一个有趣的观点,有时可能会令人困惑:类型应该从右到左阅读。例如,这是(从最右边开始*
)一个指向 int 常量指针的指针。
int * const *x;
What you wrote would therefore be a pointer to a reference, which is not possible.
因此,您编写的内容将是指向引用的指针,这是不可能的。
回答by serg06
Here, Erik explains every way pass an array by reference: https://stackoverflow.com/a/5724184/5090928.
在这里,Erik 解释了通过引用传递数组的每一种方式:https: //stackoverflow.com/a/5724184/5090928。
Similarly, you can create an array reference variablelike so:
同样,您可以创建一个数组引用变量,如下所示:
int arr1[] = {1, 2, 3, 4, 5};
int(&arr2)[5] = arr1;