我在哪里使用赋值运算符?

时间:2013-10-19 00:01:48

标签: c++ arrays class assignment-operator

我对课程很陌生,虽然我编写了所有其他代码,但在我的两个成员函数结束时,我仍然缺乏一些实现。

这是我的标题:

class bignum
{
public:
// Constructors.
bignum();
bignum(int num_digits);
bignum(const string &digits);
bignum(const bignum &other);

// Destructors.
~bignum();

// Assignment operator.
bignum &operator=(const bignum &other);

// Accessors
int digits() const;
int as_int() const;
string as_string() const;
void print(ostream &out) const;
bignum add(const bignum &other) const;
bignum multiply(const bignum &other) const;
bool equals(const bignum &other) const;
int PublicNumberTest;

private:
// Pointer to a dynamically-allocated array of integers.  

int *digit;

// Number of digits in the array, not counting leading zeros.

int ndigits;
}; 
#endif

这是我的一个成员函数:

bignum bignum::multiply(const bignum& other) const{
bignum product;
bignum row;
int carry = 0;
int sum = 0;
int j = 0;
int *temp_row = new int[];
for (int i = 0; i < ndigits-1; i++){
    carry = 0;
    temp_row[i] = 0;
    for (j; j < other.digits - 1; j++){
        sum = digit[i] * other.digit[j] + carry;
        temp_row[i + j] = sum % 10;
        carry = sum / 10;
    }
    if (carry>0)
        temp_row[i + j] = carry;
    row = row operator+temp_row //This is what I don't understand. How can I 
        product = product.add(row);  //assign the contents of temp_row?

}
}

还有另一个,但它基本上是同一个问题。我有一个数组,我想复制到我的...类的内容和内部?我猜?谢谢阅读。

3 个答案:

答案 0 :(得分:0)

如果您想使用temp_row[]作为this对象的数字集,您可以这样做:

delete [] digits;
digits = new_row;

您也应该更新ndigits

答案 1 :(得分:0)

这样做的两种方式:“旧”方式,它应该与调用析构函数然后复制构造函数非常相似:

bignum& bignum::operator=(const bignum& other)
{
    delete[] digit;
    ndigits = other.ndigits;
    digit = new int[ndigits];
    for (int i = 0; i != ndigits; ++i) {
         digit[i] = other.digit[i];
    }
    return *this;
}

代码,也可以写成:

bignum& bignum::operator=(const bignum& other)
{
    this->~bignum();
    new(this) bignum(other);
    return *this;
}

现在不鼓励这种陈旧的方式,因为不是例外安全。

“新”方式,这是异常安全的:你定义一个从不抛出任何异常并使用它来实现operator =()的swap()成员方法:

void bignum::swap(bignum& other) noexcept
{
    std::swap(ndigits, other.ndigits);
    std::swap(digit, other.digit);
}

bignum& bignum::operator=(const bignum& other)
{
    bignum tmp(other);
    this->swap(tmp);
    return *this;
}

答案 2 :(得分:0)

我只是留下了一些难题,而不是计算出你的多位数乘法,

  

您要执行c = a b还是a = a b?也就是说,你打算   将两个参数相乘,生成一个临时参数,并将其分配给第三个参数,   或者你打算将对象a乘以参数b?

     

我的意图是C = A * B

     

然后你需要分配(新)目的地bignum,

使用friend运算符重载应解决您的需求;定义二元运算符是常见的习惯用法(注意我没有解决你分配正确数字的数字),

friend bignum operator+(const bignum& a, const bignum& b); //in class

//you want to calculate A * B --> C
//you need a temporary place to calculate C, then produce a new C,
bignum bignum::multiply( const bignum& A, const bignum& B ) const
{
    //Your A is this, A.digit[];
    //Your B is other, B.digit[];
    //Yout C is temp, a buffer for calculations,
    bignum temp;

    int carry = 0;
    int sum = 0;
    int j = 0;
    //you don't need this, you have temp.digit[];
    for (int i = 0; i < this->ndigits-1; i++)
    {
        carry = 0;
        temp.digit[i] = 0;
        //what do you want to initialize j to?
        for (j; j < B.ndigits - 1; j++)
        {
            sum = A.digit[i] * B.digit[j] + carry;
            temp.digit[i + j] = sum % 10;
            carry = sum / 10;
        }
        if (carry>0)
            temp.digit[i + j] = carry;
    }
    return bignum(temp); //allocate a new bignum
}

有关如何重载二元运算符的示例,请参阅here ...

你做的方式有缺点(返回一个const,不能链接两个以上的添加),但基本上你做了类似的事情,但使用(this->)指针,

//you want to calculate A * B --> C
//you need a temporary place to calculate C, then produce a new C,
bignum bignum::multiply(const bignum& other) const
{
    //Your A is this, this->digit[];
    //Your B is other
    //Yout C is temp product, a buffer for calculations,
    bignum temp;

    int carry = 0;
    int sum = 0;
    int j = 0;
    for (int i = 0; i < this->ndigits-1; i++)
    {
        carry = 0;
        temp.digit[i] = 0;
        for (j; j < other.ndigits - 1; j++)
        {
            sum = this->digit[i] * other.digit[j] + carry;
            temp.digit[i + j] = sum % 10;
            carry = sum / 10;
        }
        if (carry>0)
            temp.digit[i + j] = carry;
    }
    //bignum product;
    //return *this = temp; //do this,
    return bignum(temp); //or you should probably return a new object
}

你应该return a new object