以下代码无法编译.
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 &模拟引用数组.