简体   繁体   English

如何在不使用指针的情况下编写对10个整数的数组的引用?

[英]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.

相关问题 如何在不使用向量的情况下创建指针数组 - How to create an array of pointers without using vector 使用引用/指针调整数组大小 - Array resizing using reference / pointers 如何不使用指针将动态2D数组传递给函数? - how to pass dynamic 2d array to a function without using the pointers? 如何在不使用向量的情况下调整动态指针数组的大小 - How can I resize a dynamic array of pointers without using a vector 如何在不使用指针的情况下在 function 中制作二维动态数组 - how to make a 2D Dyanmic array in function without using pointers 如何在不使用循环的情况下快速打印由指针组成的2个字符的二维数组? - How can I quickly printf 2 dimensional array of chars made of pointers to pointers without using a loop? 通过引用返回指针数组 - Return array of pointers by reference 如何在不使用原始指针的情况下为 class 类型编写自定义 STL 迭代器? 这有实际优势吗? - How can I write a custom STL iterator for a class type without using raw pointers? Is there a practical advantage to this? 如何使用指针为矩阵编写单循环 - How to write single loop for a matrix using pointers 有没有办法在不使用指针的情况下通过引用将对象添加到向量? - Is there a way to add objects to a vector by reference without using pointers?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM