0

I would like to implement the + sign to add the my_Contents (int) of two class Mailbox's 'left' and 'right'.

ex.

Mailbox(test);
Mailbox(left); 
Mailbox(right);
left.setSize( 10 ); //my_contents=10
right.setSize( 10 );//"" "" = 5
test.setSize( 5 );// "" ""=5
test = left + right;

However, class Mailbox is NOT initialized as Mailbox 'Name' (my_contents). Therefore my code below will not run. So how would I implement the operator '+' to apply to contents inside the class, which aren't used for initialization, as I am used to?

Mailbox operator +(const Mailbox& left,
              const Mailbox& right) {
Mailbox t =Mailbox( left.my_Contents + right.my_Contents );
return( t );
}
Rachel
  • 1
  • 2
  • Uh, could you try explaining that again in more detail? – Brian Bi Jul 27 '14 at 17:59
  • One normally defines `operator+` in terms of `operator+=`. – chris Jul 27 '14 at 18:04
  • I'm a beginner, could you go into more detail about operator +=? @Brian I added more code if it helps. – Rachel Jul 27 '14 at 18:08
  • `Mailbox(test), Mailbox(left), Mailbox(right);` does not declare three variables. Use `Mailbox test; Mailbox left; Mailbox right;`, or `Mailbox test, left, right;` to condense it (which can be considered a bad practice). For more info on `operator+=` and how to implement both it and `operator+`, see [operator overloading](http://stackoverflow.com/questions/4421706/operator-overloading). – chris Jul 27 '14 at 18:11
  • What do you mean by "My code will not run"? Does it not compile? Does it crash? Does it not do what you want? – user93353 Jul 27 '14 at 18:15
  • The functional style cast of int (my_Contents) to the Mailbox object t cannot be converted. – Rachel Jul 27 '14 at 18:20
  • This is probably just some toy program to learn operator overloading, but I don't think adding two mailboxes makes sense in the first place, i.e. we have a classical case of operator-overloading abuse. – Christian Hackl Jul 27 '14 at 18:34

2 Answers2

1

1) Call the setSize to set my_Contents of the result.

Mailbox operator +(const Mailbox& left, const Mailbox& right) 
{
    int s = left.my_Contents + right.my_Contents;
    Mailbox t;
    t.setSize(s);
    return t;
}

Another thing is that your operator needs to be a friend of class Mailbox

i.e.

class Mailbox
{
    int my_contents;

    public:
    void setSize(int x) ;

    // Whatever else

    friend Mailbox operator +(const Mailbox& left, const Mailbox& right);
};

2) or if you don't want to make it a friend you, have a getSize method

Mailbox operator +(const Mailbox& left, const Mailbox& right) 
{
    int s = left.getSize() + right.getSize();
    Mailbox t;
    t.setSize(s);
    return t;
}

where getSize is a member method

void getSize()
{
    return my_Contents;
}

3) Or you could implement it in terms of +=

class Mailbox
{
    int my_Contents;
    public:

    Mailbox & operator +=(const Mailbox & r)
    {
        my_Contents += r.my_Contents;
        return *this;
    }
    // Whatever else

};

Mailbox operator +(const Mailbox& left, const Mailbox& right) 
{
    Mailbox t = left;
    t += right;
    return t;
}
user93353
  • 13,733
  • 8
  • 60
  • 122
0

This seems to accomplish what you are looking for:

class Mailbox
{
public:
  Mailbox() {}
  Mailbox(int x) : my_contents(x) {}
  void SetSize(int x) 
  {
    my_contents = x;
  }
  int my_contents;
};

static Mailbox operator+(Mailbox& left, Mailbox& right)
{
  return Mailbox(left.my_contents + right.my_contents);
}

Mailbox test;
Mailbox left(10);
Mailbox right(5);
test = left + right;

And it does compile with the proper surroundings.

Logicrat
  • 4,438
  • 16
  • 22