为什么在C ++中不允许使用引用数组 [英] Why array of references not allowed in C++

查看:343
本文介绍了为什么在C ++中不允许使用引用数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的代码如下:

 #include< iostream> 
using namespace std;
int main(){
int ix,iy,iz;
int * ixPtr =& ix,*& ixPtrRef = ixPtr;
//以下语句标记编译器错误
int& intArrayOfRefs [] = {ix,iy,iz};
返回0;
}
>< / iostream>



但是为什么C ++中不允许引用数组

 错误 C2234:'  iArrayOfRefs':引用数组是非法的 

解决方案

基本上,引用是现有变量的别名。这意味着,如果对引用应用任何操作,则其行为就像使用原始变量名一样。这个原则也适用于获取它的地址 - 结果,如果你取一个引用的地址,你将得到一个指向引用变量的指针,正如你想的那样,而不是你获取一个指向包含变量值的地址的指针:

 int original = 5; 
int& alias = original; //指5
int * p_original =& original; //让我们假设:p_original == 0x01231234
int * p_alias =& alias; // p_alias == 0x01231234
(int&)* pr_alias =& alias; //错误! (& alias的类型为int *)



如果要创建引用数组,则数组符号本身将被视为指向数组元素的指针。由于获取引用的地址会产生指向原始元素的指针,因此指针的类型将是指向原始元素类型的指针,而不是指向(...)的引用的指针。因此,使用引用作为数组的元素类型被认为是非法的。



(我不得不承认我对最后一部分并不完全满意,但我'我从来没有遇到过更好的解释。)


顺便设计一种语言,引用是其他内容的别名,必须在实例化时初始化。 br />
但该语言没有提供任何一起初始化阵列元素的机制。



唯一可行的方法应该是某种语言扩展允许诸如

  int  a,b,c; 
int& v [] = {a,b,c};
// v [0]别名a,v [1] b和v [2]做c 。



但是ab& amp; c不是常数。


看这里:

类似讨论 - 1 [ ^ ]

MSDN发言.. 。 [ ^ ]

Hi,
My code looks like this:

#include <iostream>
     using namespace std;
     int main() {
      int ix,iy,iz;
      int *ixPtr = &ix,*&ixPtrRef=ixPtr;
      //the following statement flags a compiler error
      int &intArrayOfRefs[] = {ix,iy,iz};
      return 0;
     }
></iostream>


But why is array of references is not allowed in C++

error C2234: 'iArrayOfRefs' : arrays of references are illegal  

解决方案

Basically, a reference is an alias to an existing variable. This means, if you apply any operation on a reference, it will behave as if you were using the original variable name. This principle also applies to taking the address of it - and as a result, if you take the address of a reference, you will not get a pointer to the reference variable, as you might think, instead you get a pointer to the address that holds the variables value:

int original = 5;
int& alias = original; // refers to 5
int* p_original = &original; // let's assume that: p_original == 0x01231234
int* p_alias = &alias; // p_alias == 0x01231234
(int&)* pr_alias = &alias; // error! (&alias has the type int*)


If you wanted to create an array of references, the array symbol itself would be considered a pointer to an array element. Since taking the address of a reference yields a pointer to the original element, the type of the pointer will be pointer to original element type, not pointer to reference of (...). Therefore using a reference as an element type for an array is considered illegal.

( I have to admit I'm not entirely happy with that last part, but I've never come across a better explanation )


By the way the language is designed, a reference is an alias to something else and must be initialized on instantiation.
But the language doesn't provide any mechanism to initialize array elemets all together.

The only possible way should be a sort of language extension allowing things like

int a,b,c;
int& v[] = { a, b, c };
// v[0] aliases a, v[1] does b and v[2] does c.


But that's not feasible being a b & c not constant.


Look here:
Similar discussion - 1[^]
MSDN speaks...[^]


这篇关于为什么在C ++中不允许使用引用数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆