Skip to content

Commit

Permalink
Implement = operator for BigInteger
Browse files Browse the repository at this point in the history
There's a copy constructor, but no '=' operator implemented. This is dangerous.
  • Loading branch information
blackball committed Aug 7, 2015
1 parent fc50f10 commit c085447
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion include/rapidjson/internal/biginteger.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,14 @@ class BigInteger {
if (length > 0)
AppendDecimal64(decimals + i, decimals + i + length);
}


BigInteger& operator=(const BigInteger &rhs)
{
count_ = rhs.count_;
std::memcpy(digits_, rhs.digits_, count_ * sizeof(Type));
return *this;
}

BigInteger& operator=(uint64_t u) {
digits_[0] = u;
count_ = 1;
Expand Down

3 comments on commit c085447

@cosinekitty
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a good idea to check for self-assignment -- to make sure (&rhs != this) -- before modifying anything.
It might be fine here, depending on how memcpy works on various systems when passed the same pointer for first and second arguments.
See:
https://isocpp.org/wiki/faq/assignment-operators

@pah
Copy link
Contributor

@pah pah commented on c085447 Aug 13, 2015

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 on this one.

@blackball
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Please sign in to comment.