[英]How to write a reference to an array of 10 integers without using pointers?
Sorry if this is a really dumb question, but for some reason I can't understand how to do it. 抱歉,这是一个非常愚蠢的问题,但是由于某种原因,我不知道该怎么做。
I tried to write code that makes the most sense for me, but it's not working: 我试图编写对我来说最有意义的代码,但是它不起作用:
int numbers[10]{};
int& numbers_ref { numbers }; // nope
int numbers_ref& { numbers }; // nope
int& numbers_ref { &numbers }; // nope
int numbers_ref& { &numbers }; // nope
Edit: I tried using []
but I still couldn't get it working without using auto
: 编辑:我尝试使用
[]
但如果不使用auto
仍然无法正常工作:
int& numbers_ref[] { numbers }; // nope
(int numbers_ref[])& { numbers }; // nope
(int numbers_ref[10]) & { numbers }; // nope
int []numbers_ref& { &numbers }; // nope
The only way I can get a reference is what P0W said: 我可以获得参考的唯一方法是P0W所说的:
auto& numbers_ref= numbers; // yay
PS Thank you chris , this is exactly what I wanted, but couldn't guess the syntax: PS谢谢chris ,这正是我想要的,但是无法猜出语法:
int (&numbers_ref)[10] = numbers; // exactly right!
typedef int numbers_type[10];
numbers_type& numbers_ref = numbers;
Edit: (As suggested by chris) 编辑:(如克里斯建议)
int (&numbers_ref) [10] = numbers; // If you don't want typedef
In C++11 : 在C ++ 11中:
auto& numbers_ref= numbers;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.