注意:我正在使用g ++编译器(听说很好,应该与标准相当)。
比方说,您已经声明了一个整数数组:
int a[3] = { 4, 5, 6 };
]现在让我们说您真的要声明对该数组的引用(不要介意为什么,除了Bjarne表示该语言支持该数组之外)。
情况1 –如果您尝试:
int*& ra = a;
然后,编译器不愿说:
"invalid initialization of non-const reference of type `int*&' from a temporary of type `int*'"
首先,为什么“ a”是一个临时变量(即它在内存中没有位置?)...
无论如何,只要我看到非常量错误,我都会尝试 放入一个常量...
情况2 –如果您尝试:
int*const&rca = a; //wish I knew where the spaces should go (but my other post asking about this sort of protocol got a negative rank while many of the answers got ranked highly -- aha! there are stupid questions!)
然后一切都很好,它会编译,并且您会得到对该数组的引用。
情况3-现在这里是另一件事 将会编译:
int* justSomeIntPointer = a; //LINE 1
int*& rpa = justSomeIntPointer; //LINE 2
这也为您提供了对原始数组的引用。
所以这是我的问题:静态声明的数组的名称在什么时候出现 成为const指针? 我似乎记得一个整数数组的名称也是一个指向int的指针,但是我不记得它曾经是一个const-pointer-to-int ...
似乎情况1失败了 因为声明(ra)的引用不是指向const指针,这可能意味着'a'已经是一个从const指针到int的开头。
情况2似乎起作用,因为引用声明为( [rca)已经是一个const-pointer-to-int。
情况3也可以使用,这很简洁,但是为什么呢? 假定的指向int的指针(即数组名称“ a”)在什么时候成为const指针? 将它分配给int *(第1行)时会发生这种情况,还是将int *分配给int *&(第2行)时发生了吗?
希望这很有意义。 谢谢。