| <<< | Index | >>> |
It's common to define two operator[]s
Allows use on left hand side, as well as on const objects:
template< typename ElementT >
class MyArray {
public:
ElementT& operator[](unsigned int idx);
ElementT operator[](unsigned int idx) const;
//...
};//class MyArray
void copy_element( unsigned int idx, MyArray<int> const& src, MyArray<int>& dest )
{
// Uses non-const op[] on left, const op[] on right:
dest[ idx ] = src[ idx ];
}
void main()
{
MyArray<int> source;
MyArray<int> destination;
//...
copy_element( 0, source, destination );
}
| <<< | Index | >>> |