I have the following:
template <typename T, unsigned int N>
class MyVector {
protected:
std::vector<TmVec;
public:
MyVector() : mVec(N,T()){}
// (1)
T& operator[] (unsigned int i) {
std::cout << "blop1\n";
return mVec[i];
}
// (2)
T const& operator[] (unsigned int i) const {
std::cout << "blop2\n";
return mVec[i];
}
....
....
When I do something like:
MyVector<int,3bob;
bob[2] = 77;
int a = bob[2];
only "blop1" gets printed.
What are the difference between returning a reference to T and a
reference to a const T? It seems that there is no reason to keep (2).