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

前端 未结 2 1803
误落风尘
误落风尘 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:38

    From the standard:

    13.3.3 If a best viable function exists and is unique, overload resolution succeeds and produces it as the result. Otherwise overload resolution fails and the invocation is ill-formed. When overload resolution succeeds, and the best viable function is not accessible (Clause 11) in the context in which it is used, the program is ill-formed.

提交回复
热议问题