我无法理解以下代码 (pointer or value) 中函数返回的内容。
#include<iostream>
using namespace std;
class safearay
{
int a[10];
public:
int& operator [](int n)
{
if(n<0 || n>5 )
{
cout<<"Exceeded bounds"<<endl;
}
return a[n];
}
};
int main()
{
safearay sa1;
for (int j=0;j<10;j++)
{
sa1[j]=j*j;
}
for (int j=0;j<10;j++)
{
int u=sa1[j];
cout<<u<<endl;
}
}
请解释
最佳答案
int& operator [](int n)
返回对 int
的引用。
本质上,您可以将它用作 int
,但更改也会影响类中的 a[n]
(因为它已返回)。它就像一个没有所有 *
的指针,只是您不能更改它指向的地址。
关于c++ - 调试 C++ 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33314842/