Sorry for the format, I never really posted to a forum like this, so I have to learn the how to a bit.
My problem is:
I'm writing a template class, and I'd like to access my containers via multiple kind of []
operators. I read a bit in this subject, so I've been able to make one overloading, but I need some more:
So in my header file, relevant things about my container:
template <class T>
class version_controlled_vector
{
int rev;
bool vector_state_changed;
std::vector< std::string > revision;
std::vector< std::vector<T> > v;
//first one works ok, im satisfied with it:
std::vector<T>& operator[] (const int idx)
{
return v[idx];
}
//im not sure how to define the further one(s?):
T& operator[](const int idx2) const
{
return v[idx2];
}
//...and ofc some other code
};
//to have these usages at my main.cpp:
version_controlled_vector<int> mi;
version_controlled_vector<std::string> ms;
//this works, and i d like to keep it,
5 == mi[ 0 ][ 0 ];
//and i d like to have these two usages too:
//getting the first character of the stored string:
'H' == ms[ 0 ][ 0 ]; // with the first overload from the header ms[0][0][0]
works to get the first character of the string for eg "Hello"
but, i have to use the ms[0][0] format to achieve this
//and this:
4 == mi[ 0 ]; // i d like this as if it d behave like 4 == mi[0][0];
I don't really get how can I use the single[]
when I made an overload to use the [][]
The only solution I have read about is maybe const-overloading, but I'm not sure at all, I'm quite a weakie.
Thanks for ideas!