This is probably a duplicate question but I can't seem to find the answer... I'm trying to implement a slicable container like object in C++ using the operator[]
. Here's my implementation (which doesn't work):
class SlicableIntVector{
private:
std::vector<int> vector;
public:
explicit SlicableIntVector(std::vector<int> vector): vector(vector){};
// The usual way of overloading operator[]
int &operator[](int index){
return vector[index];
}
// This version doesn't work as the argument is not binary
int &operator[](int start, int end){
return vector[vector.begin()+start, vector.begin()+start+end];
}
}
};
Other than just using another method to implement the slicing operation, what options do I have to get slicing working with operator[]
? If I could get the flexibility of slicing like a python list
I'd be happy.