//class Foo
class Foo
{
public:
Foo():data(10,1) {}
int& operator[] (const std::size_t);
const int& operator[](const std::size_t) const;
private:
std::vector<int> data;
};
int& Foo::operator[] (const std::size_t index)
{
std::cout<<"operator[] of Foo is called"<<std::endl;
return data[index];
}
const int& Foo::operator[](const std::size_t index) const
{
std::cout<<"operator[] const of Foo is called"<<std::endl;
return data[index];
}
<pre name="code" class="cpp"> // test operator[]
Foo f;
std::cout<<f[2]<<std::endl;
f[1] = 3;