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

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

    It's worth mentioning that c++ allows const methods/functions overloading (e.g. foo() const), but not const arguments overloading (e.g. bar(int a) and bar(const int a)).

    0 讨论(0)
  • 2020-11-29 11:41

    In the example you gave:

    vector<int>::const_iterator it = myvector.begin();
    

    if myvector isn't const the non-const version of begin() will be called and you will be relying on an implicit conversion from iterator to const_iterator.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 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<int>& myvector )
    {
        vector<int>::const_iterator it = myvector.begin(); //const version will be called
    }
    
    vector<int> myvector;    
    vector<int>::const_iterator it = myvector.begin(); //non-const version will be called
    
    0 讨论(0)
  • 2020-11-29 11:46

    The compiler's "algorithm" is like this: Every member function of class X has an implicit argument of type X& (I know, most think it's X*, but the standard states, that for purposes of Overload Resolution we assume it to be a reference). For const functions, the type of the argument is const X&. Thus the algorithm, if a member function is called the two versions, const and non-const, are both viable candidates, and the best match is selected just as in other cases of overload resolution. No magic :)

    0 讨论(0)
  • 2020-11-29 11:58

    The compiler determines if the object variable is const or not at compile time

    It then picks the corresponding overload, and whatever return type it has.

    class C {
        public:
            int f() { return 1; }
            float f() const { return 1.5; }
    };
    
    // Non const.
    C c;
    assert(c.f() == 1);
    
    // Convert variable const at compile time.
    assert(const_cast<const C&>(c).f() == 1.5);
    
    // Same as above but with an explicit reference.
    const C& d = c;
    assert(d.f() == 1.5);
    
    // Analogous but with a new const object from the start.
    const C e;
    assert(d.f() == 1.5);
    
    0 讨论(0)
提交回复
热议问题