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

后端 未结 6 1544
孤街浪徒
孤街浪徒 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:45

    Yes, the const modifier affects overloading. If myvector is const at that point const version will be called:

    void stuff( const vector& myvector )
    {
        vector::const_iterator it = myvector.begin(); //const version will be called
    }
    
    vector myvector;    
    vector::const_iterator it = myvector.begin(); //non-const version will be called
    

提交回复
热议问题