【问题标题】:Overloading postfix increment operator重载后缀增量运算符
【发布时间】:2010-11-09 03:35:49
【问题描述】:

我正在尝试将后缀增量运算符重载为将大数存储为整数数组的类的成员函数。但它一直返回为 0。关于为什么这不起作用的任何提示?

这是一个家庭作业问题,所以我想要更多的提示而不是直接的代码。谢谢。

会员资料如下:

largeInt = new int[maxSize];
int maxSize, currentSize;

其中 currentSize 是一个跟踪变量,用于跟踪当前数组的大小。

我的代码是:

Load 函数在数组的第一个位置放置一个 int 并将其他所有内容移过来。

/* postfix*/
NewInt& NewInt::operator++(int nothing)
{   
    int count = 1;
    largeInt[currentSize - count] += 1;
    while(largeInt[currentSize - count] > 9)
    {
            if(currentSize - count - 1 < 0)
            {
                    firstVar = true;
                    Load(1);
            }
            else    
                    largeInt[currentSize - count - 1] += 1;

            count++;              
    }

    return *this;
}   

【问题讨论】:

  • 您显示 LargeInt 但实际代码使用“数字”。 number 是对 LargeInt 的引用吗
  • operator++的参数不要命名;这是最佳实践
  • 是的,我把它改成了largeInt,在我的程序中是正确的,只是有点累,从一个测试程序中复制粘贴。固定。
  • Chubsdad 表示名为“nothing”的参数。

标签: c++ arrays operator-overloading


【解决方案1】:

您的评论与您的​​代码不一致。 operator++(int) 是后缀增量,operator++() 是前缀。

【讨论】:

  • 糟糕,无论如何我都需要做这两件事。我将其编辑为后缀
  • 至少部分。确实,99++ 给出 110。
  • 我被那个前置/后缀挂断了。想通了其他的东西。谢谢。
猜你喜欢
  • 2012-04-13
  • 1970-01-01
  • 2011-03-12
  • 2010-10-14
  • 2013-07-27
  • 2011-05-19
  • 1970-01-01
相关资源
最近更新 更多