我写了这样的代码:
void Print(const int & dataArray[], const int & arraySize) { // problem
for(int i = 0; i<arraySize; i++) {
cout << dataArray[i] << " ";
}
cout << endl;
}
Run Code Online (Sandbox Code Playgroud)
在mian()函数中:
`
int iArray[14] = { 7, 3, 32, 2, 55, 34, 6, 13, 29, 22, 11, 9, 1, 5 };
int numArrays = 14;
Print(iArray, numArrays);
....
`
Run Code Online (Sandbox Code Playgroud)
编译器说引用数组是非法的,为什么它是非法的?我看到<effective c ++>,它说建议我们使用const和reference,我只是尝试实现它(我是初学者),我也想知道void Print(const int dataArray[], const int & arraySize)参数我用const,&来限定arraySize,是不是?(或者它比int arraySize或const int arraySize好多了?),我也想使用const,&to dataArray [],但是我失败了.
数组要求其元素是可默认构造的,而引用则不是,因此引用数组是非法的.这个:
const int & dataArray[]
Run Code Online (Sandbox Code Playgroud)
是一系列参考文献.如果你想要一个数组的引用,你需要这个:
const int (&dataArray)[]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1623 次 |
| 最近记录: |