Example
class MyMap {
std::map<K,V> m_map;
public:
void myFunc( K const& a, V const& b ) {
// want to use [] operator on the current object.
// something like this->[a] = b;
}
V const& operator[]( K const& key ) const {
//body
}
}
How do I access the MyMap with a given key in the function myFunc() using operator[] ?
You can dereference on this, e.g.
void myFunc( K const& a, V const& b ) {
(*this)[a];
}
or call the operator[] in function-call syntax:
void myFunc( K const& a, V const& b ) {
this->operator[](a);
}
BTW: The overloaded operator[] returns V const&, on which you can't perform assignment.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With