为什么要引用数组是非法的? [英] Why arrays of references are illegal?

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

问题描述

以下code不能编译。

The following code does not compile.

int a = 1, b = 2, c = 3;
int& arr[] = {a,b,c,8};

什么的C ++标准说这个呢?

P.S。我知道我可以声明包含一个参考和阵列中使用它的类,但我真的想知道为什么code以上不能编译。

P.S. I know I could declare a class that contains a reference and use it in array, but I really want to know why the code above doesn't compile.

编辑:以下code是我的问题一个很好的办法。

The following code is a good workaround for my question.


struct cintref
{
    cintref(const int & ref) : ref(ref) {}
    operator const int &() { return ref; }
private:
    const int & ref;
    void operator=(const cintref &);
};

int main() 
{
  int a=1,b=2,c=3;
  //typedef const int &  cintref;
  cintref arr[] = {a,b,c,8};
}

有可能使用结构cintref 而不是 const int的&安培; 如果你想创建数组引用。

It is possible to use struct cintref instead of const int & if you want to create the array of references.

推荐答案

C ++标准8.3.2 / 4

有应当是没有引用任何引用,的引用的阵列,并没有指针引用。

There shall be no references to references, no arrays of references, and no pointers to references.

这篇关于为什么要引用数组是非法的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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