1

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

Community
  • 1
  • 1
Nayef
  • 364
  • 2
  • 8
  • 22

2 Answers2

3

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).

NPE
  • 486,780
  • 108
  • 951
  • 1,012
  • @ravi: You mean this? http://stackoverflow.com/questions/8716330/purpose-of-returning-by-const-value – NPE Dec 25 '14 at 08:05
  • @ravi and if so, [consider this](http://stackoverflow.com/questions/7138780/move-semantics-returning-const-values) – WhozCraig Dec 25 '14 at 08:05
  • I have seen these articles...But if a,b,c were ints, then a+b = c would give you error. So, why to make diversion from standard behavior...One rule of operator overloading is to preserve natural semantics...isn't it. – ravi Dec 25 '14 at 08:08
  • @ravi If you want to disallow assignment to class rvalues, you can simply add the `&` qualifier to the class's `operator=` (as of C++11, anyway). –  Dec 26 '14 at 11:38
1
// 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
Adolfo
  • 281
  • 2
  • 5