How to make an overloaded [] operator that you can read but also write to?

Hello,

I am making a vector class. I am confused how to make an overlaoded [] operator that I can also write to.

Currently, this is my [] operators...

string myStringVector::operator[](int index){
return *(base+index);
}

string myStringVector::operator[](int index) const{
return *(base+index);
}

I am trying to make a statement like...

v[0] = "a" assign a to index 0 of my vector v. I've tried looking this topic up but its all just confusing if someone could just help me understand how this works I'd appreciate it.

Thanks!
Last edited on
1
2
3
4
5
6
7
8
9
std::string  myStringVector::operator[]( std::size_t index ) const { // return a value (a copy)
        
    return base[index] ;
}

std::string&  myStringVector::operator[]( std::size_t index ) { // return an object (a reference)
        
    return base[index] ;
}
Thanks!
Topic archived. No new replies allowed.