65,186
社区成员




class Foo
{
public:
Foo()
{
for (int i = 0; i < 10; i++)
{
data.push_back(i);
}
}
int &operator[] (const size_t);
const int &operator[] (const size_t) const;
// other interface members
private:
std::vector<int> data;
// other member data and private utility functions
};
// 下标操作符本身可能看起来像这样:
int& Foo::operator[] (const size_t index)
{
return data[index]; // no range checking on index
}
const int& Foo::operator[] (const size_t index) const
{
return data[index]; // no range checking on index
}
int _tmain(int argc, _TCHAR* argv[])
{
Foo a;
a[0] = 145; // 作左值
int n = a[0]; // 作右值
return 0;
}