-2

I have simple class which holds array of integers. I want to overload + to merge two ararys together. I have defined two overloadors. + and =

class Money{

    Money & operator =( const Money &a )
    {
        for( int i = 0; i < size ;i++ ) arr[i] = a.arr[i];

        return *this;
    }
    Money & operator +( const Mone &a )
    {
        Money temp;

        for( int i = 0; i < size  ;i++ ){
            temp.arr[i] =  arr[i] + a.arr[i];
        }

        return temp;
    }

private:
  int arr[50];
  int size = 50;
}

The problem is index 0, it returns random number from memory . I have seen some question about similliar problem but with * operator ( i will try to find it and link it ), where i got my operator = from. What is causing this? I am invoking it as

Money a;
Money b;
Money d;
d = a + b;
// print array;

I am new to overloading so i have harder time analyzing and understaning concept.

Darlyn
  • 4,715
  • 12
  • 40
  • 90
  • Not to be rude, but your concept for a `Money` class seems to be flawed from the bone. Why do you need an array there?? – πάντα ῥεῖ Apr 03 '16 at 23:07
  • This concept of class is just for practising overloading. – Darlyn Apr 03 '16 at 23:08
  • @trolkura What are m_size and one? Did you compile your code? Show minimal compiled and verified program. – Vlad from Moscow Apr 03 '16 at 23:10
  • 1
    @trolkura Regarding `return temp;` see [Can a local variable's memory be accessed outside its scope?](http://stackoverflow.com/questions/6441218/can-a-local-variables-memory-be-accessed-outside-its-scope). – πάντα ῥεῖ Apr 03 '16 at 23:10
  • fixed , the code , miss-typed name of the variables, sorry for that – Darlyn Apr 03 '16 at 23:14
  • @trolkura Again, your code is completely nonsensical, and you don't provide any semantical context for what you actually want to achieve. May be reading about [Operator Overloading](http://stackoverflow.com/questions/4421706/operator-overloading) helps you out. – πάντα ῥεῖ Apr 03 '16 at 23:17
  • As I understand the problem in operator + you are creating a temporary variable temp. That variable will live until the code of "operator+" is finished. As you are returning a reference to that variable id est essentially a hidden pointer to it when the code finishes that reference isn't any more valid. If instead you change the code and return by value everything should work correctly. Instead things work correctly for the =operator because the reference you return is tied to an object that will exist after the end of the "operator=" call. – George Kourtis Apr 03 '16 at 23:33
  • @πάνταῥεῖ that thread really has a misleading title... local var's memory certainly can be accessed outside its scope, but not outside its lifetime – M.M Apr 03 '16 at 23:36

2 Answers2

2

The basic problem is here, and has nothing really to do with overloading:

Money & operator +( const Mone &a ) {
    Money temp;
        :
    return temp;

The problem being that you return a reference to a local variable, which is destroyed when the function returns so is a dangling reference, leading to undefined behavior.

Change it to return Money by value.

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
1

As I understand the problem in operator + you are creating a temporary variable temp. That variable will live until the code of "operator+" is finished. As you are returning a reference to that variable id est essentially a hidden pointer to it when the code finishes that reference isn't any more valid. If instead you change the code and return by value everything should work correctly. Instead things work correctly for the =operator because the reference you return is tied to an object that will exist after the end of the "operator=" call.

George Kourtis
  • 2,381
  • 3
  • 18
  • 28