有人可以向我解释为什么我重载的++(预版本)没有更新值吗?代码段是这样的:

circle circle:: operator++()
{
    Area = Area * 2.0;
    return *this;
}
/////////////////////////////

int main()
{
    class circle c1(4, 1, -1), c2(12, 4, 6);
    c1.output();
    c1++;
    c1.output();

    system("pause");
    return 0;
}

最佳答案

这是因为您重载了前缀并调用了后缀。您需要调用++c1;。要使用c1++;,您还需要重载后缀:

  circle operator++ ( int );

关于c++ - 重载++运算符在c++中不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36176516/

10-13 08:29