On a nonconst object, why won't C++ call the const version of a method with public-const and private-nonconst overloads?

前端 未结 2 1818
误落风尘
误落风尘 2020-12-20 10:44
class C
{
public:
    void foo() const {}
private:
    void foo() {}
};

int main()
{
    C c;
    c.foo();
}

MSVC 2013 doesn\'t like this:

2条回答
  •  天涯浪人
    2020-12-20 11:41

    The object is not const, so the non-const overload is a better match. Overload resolution happens before access checking. This ensures that overload resolution is not inadvertently changed by changing the access of a member function.

提交回复
热议问题