不编译以下代码。
int a = 1, b = 2, c = 3;
int& arr[] = {a,b,c,8};
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};
}
可以使用 struct cintref
而不是 const int &
来模拟引用数组。