N
Nafai
Hello I would like you guys to give me the best solution for this problem:
class bigInteger {
private:
vector<char> digits;
public:
...
bigInteger& operator+=(const bigInteger& x)
{ ... } // Already implemented.
// WHICH WAY DO I CHOOSE?
// Option 1
bigInteger operator+(const bigInteger& x) const
{
bigInteger y = *this; // ONE COPY
y += x;
return y; // TWO COPIES (?)
}
// Option 2
bigInteger* operator+(const bigInteger& x) const
{
bigInteger y = new bigInteger(*this);
*y += x;
return y;
} // ONLY ONE COPY, but I wouldn't like to avoid pointers, and I am
using references, pointers, and static variables mixed up everywhere.
// Option 3
bigInteger& operator+(const bigInteger& x) const
{
bigInteger y = new bigInteger(*this);
*y += x;
return *y;
} // It isn't OK. Now I miss garbage collector!
// Option 4
void add(const bigInteger& op, bigInteger& out)
{
out = *this;
out += op;
} // OK, but can't use operator+ and build arithmetic expressions
};
If you have any better solution, tell me!
Thanks.
class bigInteger {
private:
vector<char> digits;
public:
...
bigInteger& operator+=(const bigInteger& x)
{ ... } // Already implemented.
// WHICH WAY DO I CHOOSE?
// Option 1
bigInteger operator+(const bigInteger& x) const
{
bigInteger y = *this; // ONE COPY
y += x;
return y; // TWO COPIES (?)
}
// Option 2
bigInteger* operator+(const bigInteger& x) const
{
bigInteger y = new bigInteger(*this);
*y += x;
return y;
} // ONLY ONE COPY, but I wouldn't like to avoid pointers, and I am
using references, pointers, and static variables mixed up everywhere.
// Option 3
bigInteger& operator+(const bigInteger& x) const
{
bigInteger y = new bigInteger(*this);
*y += x;
return *y;
} // It isn't OK. Now I miss garbage collector!
// Option 4
void add(const bigInteger& op, bigInteger& out)
{
out = *this;
out += op;
} // OK, but can't use operator+ and build arithmetic expressions
};
If you have any better solution, tell me!
Thanks.