how does overloading of const and non-const functions work?

后端 未结 6 1546
孤街浪徒
孤街浪徒 2020-11-29 11:02

The stl is full of definitions like this:

iterator begin ();
const_iterator begin () const;

As return value does not participate in overloa

6条回答
  •  天命终不由人
    2020-11-29 11:42

    From C++ standard (§13.3.1 Candidate functions and argument lists):

    For non-static member functions, the type of the implicit object parameter is “reference to cv X” where X is the class of which the function is a member and cv is the cv-qualification on the member function declaration. [Example: for a const member function of class X, the extra parameter is assumed to have type “reference to const X”. ]

    So, in your case, if myvector object is const compiler will pick version of begin which has implicit object parameter of type reference to const vector which is const version of begin.

提交回复
热议问题