3

I have a class named ThreeDigits on c++ code. I overloaded the + operand, this way:

ThreeDigits* ThreeDigits::operator+(const ThreeDigits &number) const

{
   double result= getNumber()+number.getNumber();
   ThreeDigits* new_result=new ThreeDigits(result);
   return new_result;
}

but when I write on the main function:

    ThreeDigits* first=new ThreeDigits(2.55998);
    ThreeDigits* second=new ThreeDigits(5.666542);
    ThreeDigits* result=first+second;

I get the following compilation error: invalid operands of types ThreeDigits* and ThreeDigits* to binary operator+

Can you tell me what is the problem? thanks

fgfjhgrjr erjhm
  • 325
  • 1
  • 10
  • 24

4 Answers4

13

You are trying to sum pointers to objects instead of the objects themselves. To invoke the overloaded operator you must call it on objects, thus dereferencing the pointers.

By the way, creating all those objects with new a terrible way to do C++; in C++, unlike Java/C#, you should use new only when you have to, and allocate all the rest on the stack. Having the operator+ return a pointer to a newly created object is an abomination.

The C++ way of writing your code would be:

ThreeDigits ThreeDigits::operator+(const ThreeDigits &number) const
{
   return ThreeDigits(getNumber()+number.getNumber()); // construct a temporary and return it
}

// ...

ThreeDigits first(2.55998);
ThreeDigits second(5.666542);
ThreeDigits result=first+second;

By the way, the usual way of overloading arithmetic operators is first overloading the assigning versions (+=, -=, ...) and then build the "normal" version over them. For more info about operator overloading see the operator overloading FAQ.

Community
  • 1
  • 1
Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
  • 1
    Just curious, but why did you use `()`-style initialization in the first two lines and `=`-style initialization in the last line? – Robᵩ Sep 13 '11 at 18:30
  • @Rob: I don't really know... :) Maybe because it remained from the copy-paste of the original code, maybe because it felt more "natural" at the moment to have an expression like `x=y+z`. – Matteo Italia Sep 13 '11 at 18:37
3

To use your operator as written, you'd need to write: ThreeDigits* result=*first+*second; in order to dereference the pointers.

However, your operator has at least two problems: One, it violates the principle of least surprise in that canonically operator+ returns by value. Second, it returns a pointer to new memory that will most likely be leaked in a wide variety of cases unless care is taken.

It looks like you may come from a Java background where everything is reference counted. Instead, the idiomatic C++ implementation would look like this:

ThreeDigits ThreeDigits::operator+(const ThreeDigits &number) const
{
   double result= getNumber()+number.getNumber();
   return ThreeDigits(result);
}

And used:

ThreeDigits first(2.55998);
ThreeDigits second(5.666542);
ThreeDigits result = first+second;
Mark B
  • 95,107
  • 10
  • 109
  • 188
1

You are not dereferencing the pointers. Try this:

ThreeDigits* result = *first + *second;
Donotalo
  • 12,748
  • 25
  • 83
  • 121
1

Your operator accept two ThreeDigits references and returns a ThreeDigits pointer.

To use your operator as written, you must dereference first and second:

ThreeDigits* result = *first + *second;

This operator has an unusual signature though. operator+ should return a ThreeDigits (i.e. not a pointer to one). It's extremely bad practice for your operator to implicitly new (or malloc) data because users will not expect this. RVO means that returning a copy isn't such a big deal anyway.

Oscar Korz
  • 2,457
  • 1
  • 18
  • 18
  • ... and if his class is just a wrapper around a `double` even copying it will be much faster than a heap allocation. – Matteo Italia Sep 13 '11 at 17:41
  • True. For some reason it escaped me that we are comparing the copy to a heap allocation, which is surely a more expensive operation in this case. – Oscar Korz Sep 13 '11 at 19:33