What is the use of const overloading in C++?

前端 未结 5 2012
盖世英雄少女心
盖世英雄少女心 2020-12-12 14:30

In C++, a function\'s signature depends partly on whether or not it\'s const. This means that a class can have two member functions with identical signatures except that on

5条回答
  •  星月不相逢
    2020-12-12 14:49

    Have a look at the behaviour of std::map::operator[]. The const version throws an error if you try to reference an invalid key, but the non-const version does an insert. The insertion behaviour is much handier than having to use std::map::insert (and will do an overwrite, moreover) but can't work for a const map.

提交回复
热议问题