为什么它是不可能创造出在C ++中引用数组? [英] Why it is impossible to create an array of references in c++?
问题描述
C ++标准8.3.2 / 4说:
有应没有引用
引用,没有引用数组,
没有指针引用。
块引用>但我不明白为什么这种限制将被添加到C ++。在我看来,code波纹管可以很容易地编译和工作?这是什么限制的真正原因是什么?
INT A = 10,B = 20;
INT和C [] = {A,B};
解决方案由于指数化到一个数组中的隐式转换为指针方面实际上定义,则指针运算。因此,为了支持这一点,你必须也支持指针引用,并定义意味着他们什么指针运算。
C++ Standard 8.3.2/4 says:
There shall be no references to references, no arrays of references, and no pointers to references.
But I can't understand why this restriction is added to c++. In my opinion the code bellow can easily be compiled and work? What is the real cause of this restriction?
int a = 10, b = 20; int &c[] = {a, b};
解决方案Because indexation into an array is actually defined in terms of an implicit conversion to a pointer, then pointer arithmetic. So to support this, you'd have to also support pointers to references, and define what pointer arithmetic means on them.
这篇关于为什么它是不可能创造出在C ++中引用数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!