BigInteger Class Implementation Overload operator '+'

耗尽温柔 提交于 2019-12-13 02:33:09

问题


I want to create a bigInteger class that uses arrays in backend.

BigInt a = 4321; // assume in BigInt class: array is {4,3,2,1}
BigInt b = 2131; // assume in BignInt class: array is {2,1,3,1} 
BigInt sum = a + b; // array {6,4,5,1}

I wrote this function to overload '+' operator to add the two numbers

int* operator+(const uint32& other) const{
    uint32 sum[n];
    for(int i=0; i<n; i++){
        sum[i] = (*this[i]) + other[i];
    }
    return sum;
}

but it doesn't work.

Note: assume the array size to be fixed.

My question was not answered in Overload operator '+' to add two arrays in C++ and I made more clarification on this question.

Thank you


回答1:


The immediate problem is that you're returning a pointer to an automatic (i.e. function-local) variable. The variable goes out of scope the moment the function returns. Dereferencing the returned pointer would lead to undefined behaviour.

I would argue that the method's signature should look like this:

class BigInt {
  BigInt operator+(const BigInt& other) const {...}
  ...
}

In other words, it should take a reference to an instance of BigInt and should return another instance of BigInt (by value).




回答2:


// friend_artih_op.cpp (cX) 2014 adolfo.dimare@gmail.com
// http://stackoverflow.com/questions/27645319/
/*
    I like arithmetic operators to be 'friend' functions instead of 'class
    members' because the compiler will automatically insert invocations to
    construct a class value from literals, as it happens inside the
    'assert()' invocations in this program.
*/

 /// A small 'fake' arithmetic class
class arith {
private:
    long * m_ptr;  ///< pointed value
public:
    arith( long val=0 )
        { m_ptr = new long; *m_ptr = val; }; ///< Default constructor.
    arith( const arith& o )
        { m_ptr = new long;
        *m_ptr = *(o.m_ptr); } ///< Copy constructor.
    ~arith( ) { if (0!=m_ptr) { delete m_ptr; } } ///< Destructor.

    // operator long() const { return *m_ptr; } ///< Get stored value.
    long to_long() const { return *m_ptr; } ///< Get stored value.

    friend arith operator+(  const arith left , const arith right );
    friend bool  operator==( const arith left , const arith right );
};

inline arith operator+( const arith left , const arith right ) {
    long res = *(left.m_ptr) + *(right.m_ptr);
    // 'new long' will be called within the copy constructor
    return arith( res );
} ///< letf+rigth

inline bool operator==( const arith left , const arith right ) {
    return ( *(left.m_ptr) + *(right.m_ptr) );
} ///< letf==rigth ???

#include <cassert> // assert()
int main() {
    arith four(4);
    assert( arith(6) ==  2+four ); // 2 gets converted to arith(2)
    assert(      (6) == (2+four).to_long() );

// If you make 'operator+()' a member function, you would not have the
// compiler do the conversion (and you have to do it yourself):
    assert( arith(6) ==  arith(2)+four );
}

// EOF: friend_artih_op.cpp


来源:https://stackoverflow.com/questions/27645319/biginteger-class-implementation-overload-operator

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!