小智 5

两者都有效,但是它们做了很多不同的事情,就像引用和指针完成不同的事情一样."非常"取决于你与谁交谈,但每个人都认为他们是不同的.

int (&ref())[5];
int (*point())[5];

int (&var_ref)[5] = ref();
int (*var_point)[5] = point();
Run Code Online (Sandbox Code Playgroud)

并且使用typedef具有相同的含义,这可能会更清晰:

typedef int int5[5];

int5& ref();
int5* point();

int5 &var_ref = ref();
int5 *var_point = point();

int5 a;  // array declaration!
int5& ref() { return a; }
int5* point() { return &a; }

int main() {
  cout << var_ref[0] << '\n';       // prints 0
  cout << (*var_point)[0] << '\n';  // prints 0
}
Run Code Online (Sandbox Code Playgroud)

注意指针的额外间接,返回它并使用它.当你试图从一个切换到另一个时,你可能会把它遗漏掉,从而导致你收到的消息无效,但是没有更多的信息就无法告诉你.