为什么引用数组不合法? [英] Why are arrays of references illegal?

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

问题描述

以下代码无法编译.

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

C ++标准对此有何看法?

我知道我可以声明一个包含引用的类,然后创建该类的数组,如下所示.但是我真的很想知道为什么上面的代码无法编译.

I know I could declare a class that contains a reference, then create an array of that class, as shown below. But I really want to know why the code above doesn't compile.

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};
}

可以使用struct cintref代替const int &来模拟引用数组.

It is possible to use struct cintref instead of const int & to simulate an array of references.

推荐答案

在回答有关标准的问题时,我可以引用 C ++ Standard§ 8.3.2/4 :

Answering to your question about standard I can cite the C++ Standard §8.3.2/4:

不得包含对引用的引用,不得包含引用数组,也不得包含指向引用的指针.

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

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

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