小编bap*_*bap的帖子

在运算符的情况下C++ const转换

请考虑以下代码:

struct A {
    void operator++() const {}
};

void operator++(const A&) {}


int main () {
    const A ca;
    ++ca; // g++ Error (as expected): ambiguous overload for ‘operator++’

    A a;
    ++a; // g++ Warning: "ISO C++ says that these are ambiguous,
         // even though the worst conversion for the first is better
         // than the worst conversion for the second"
         // candidate 1: void operator++(const A&)
         // candidate 2: void A::operator++() const
}
Run Code Online (Sandbox Code Playgroud)

为什么g ++只发出警告而不是错误++a?换句话说,非成员函数如何比成员函数更合适? …

c++ g++

7
推荐指数
1
解决办法
410
查看次数

标签 统计

c++ ×1

g++ ×1