0
#include <iostream>
#include <map>
int main(void) {
  std::map<char, int> mapint;

  mapint.insert({'a', 1});
  mapint.insert({'b', 2});

  // subscript operator is overloaded to return iterator.second (the value with key 'a')
  int ex = mapint['a'];
  std::cout << ex << std::endl;
  // Why does this NOT traslate to 1=10 ?
  // instead it replaces or creates pair <'a',10>...
  mapint['a'] = 10;

  for (auto i : mapint) {
    std::cout << i.first << "," << i.second << std::endl;
  }
  
  // OUTPUT
// 1
// a,10
// b,2

  return 0;
}

How is the map operator being overloaded? I tried looking at the code for map but i couldn't find anything to answer my question... I want to make something similar for one of my classes and i think figuring this out should help alot!

8
  • en.cppreference.com/w/cpp/language/operators Commented Dec 19, 2021 at 22:04
  • Does this answer your question? What are the basic rules and idioms for operator overloading? Commented Dec 19, 2021 at 22:06
  • I am sorry but i still don't see it... Commented Dec 19, 2021 at 22:15
  • Scroll down to "Array Subscripting" in the top answer of the duplicate Commented Dec 19, 2021 at 22:16
  • 1
    mapint['a'] returns a reference to the value corresponding to key 'a'. If there was no such value, it inserts one, default-initialized; and then returns a reference to this freshly inserted value. Commented Dec 19, 2021 at 22:38

2 Answers 2

1

It basically just builds on top of existing methods found within map. It is implemented along the lines of...

template<typename Key, typename Value>
struct map {

  // This operator cannot be declared 'const', since if the key 
  // is not found, a new items is automatically added. 
  Value& operator [] (const Key& key) {
    // attempt to find the key
    auto iter = find(key);

    // if not found... 
    if(iter == end()) {
    
      // insert new item (with a default value to start with)
      iter = insert(std::make_pair(key, Value()));
    }

    // return reference to the data item stored for 'key'
    return iter->second;    
  }
};
Sign up to request clarification or add additional context in comments.

Comments

0

To overload the [] operator you can do as follows (in pseudo-code):

struct A
{
    your_return_type operator[](your_argument_list)
    {
        your implementation
    }
};

If you want to return a reference to some class member, then you may have to implement 2 versions of this operator, one const, which returns a non-modifiable reference, and one non-const, which returns a modifiable refence.

struct A
{
    your_modifiable_return_type_ref& operator[](your_argument_list)
    {
        your implementation
    }


    const your_non_modifiable_return_type_ref& operator[](your_argument_list) const
    {
        your implementation
    }
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.