相关疑难解决方法(0)

为什么引用数组是非法的?

以下代码无法编译.

int a = 1, b = 2, c = 3;
int& arr[] = {a,b,c,8};
Run Code Online (Sandbox Code Playgroud)

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

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

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};
}
Run Code Online (Sandbox Code Playgroud)

可以使用struct cintref而不是const int &模拟引用数组.

c++ arrays reference

135
推荐指数
8
解决办法
9万
查看次数

标签 统计

arrays ×1

c++ ×1

reference ×1