search

Home  >  Q&A  >  body text

c++ - 这里为什么使用引用接收返回值

hash_map<string, list<uint32_t> > g_hmLimits;
...
list<uint32_t>& lsErrorTime = g_hmLimits[strDomain];
lsErrorTime.clear();
为什么返回值是:list<uint32_t>& 类型呢?
hash_map中的value存储的是一个list对象,并不是对象的指针,这个时候返回的Value是原来Value的副本吗?

巴扎黑巴扎黑2772 days ago367

reply all(1)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-04-17 14:53:17

    First of all, I would like to emphasize that hash_map is not part of the C++ standard library, and there is no universal standard. What I said below are all based on C++11's unordered_map.
    Overloading the unordered_map operator in [] is defined as

    mapped_type& operator[] ( const key_type& k );
    mapped_type& operator[] ( key_type&& k );

    As you can see, what is returned here is a reference, so there is no problem in accepting it by reference.

    reply
    0
  • Cancelreply