C++使用引用运算符传递数组

bur*_*kim 0 c++ arrays pass-by-reference

我有一个关于使用引用运算符传递数组的问题.我想编写使用引用运算符传递数组的代码.然后我试了一下

void swap(int &testarray[3]){
 // code
}
Run Code Online (Sandbox Code Playgroud)

它给了我错误.它说,

/main.cpp:5: error: declaration of 'testarray' as array of references
Run Code Online (Sandbox Code Playgroud)

但是当改变我的代码时

void swap(int (&testarray)[3]){
  // code
  }
Run Code Online (Sandbox Code Playgroud)

它运行正常.唯一的区别是有支架.

为什么它需要括号和int(&testarray)[3]和int&testarray [3]之间有什么区别?

谢谢你的帮助.

Jar*_*d42 6

void foo(int &testarray[3])被解释为void foo((int &)testarray[3])由于优先权.引用数组是非法的.

鉴于void foo(int (&testarray)[3])您的需要解释.(引用3 int的数组).

void foo(int testarray[3])相当于void foo(int testarray[]) 衰败的void foo(int *testarray).(int指针).