return type of overload operator + exhibit weird behaviour

Computernerd

I have a class employee

#include <iostream>
#include <string>
using namespace std;

class employee
{
    public:

            double operator + (employee);
            employee(int);
            double getSalary();

    private:

           double salary;

};

int main()
{  
  employee A(400);
  employee B(800);

  cout<<A+B;

  employee C = A+B;

  cout<<C.getSalary();

}

employee::employee(int salary)
{
    this->salary = salary;
}


double employee::operator + (employee e)
{
    double total;

    total = e.salary + this->salary;

    return total;    
}


double employee::getSalary()
{
    return this->salary;
}

I have overloaded the operator + so that it adds up the salary of 2 employee objects . The return type of the overloaded + operator is double.

This is my question

1) Why does employee C = A + B work when i have overloaded the operator + to return a double and not a employee , shouldnt there be a compiler error??

2) what is actually happening???

Jerry Coffin

When you overload operator+ to return a double, the compiler will look at whether it can convert that double to an employee object. Since you have a ctor that takes an int, it does an implicit conversion from double to int, then uses your ctor to convert from int to employee.

So no, that shouldn't generate a compiler error.

At the same time, it doesn't make much sense. Right now, you've defined the employee's salary member as a double, but only allowed the user to specify an int to initialize it. You probably want to allow initializing it with a double instead.

As it stands right now, your operator+ also has asymmetric behavior: it can do implicit conversions on the right operand, but not on the left, so a + 1 works, but 1 + a doesn't.

You can eliminate all implicit conversions by making your conversion constructors (i.e., all that can be invoked with a single parameter) explicit. Conversely, you can allow implicit conversions on either a left or a right operand by implementing the overload as a free function:

employee operator+(employee const &a, employee const &b) { 
    return employee(a.salary + b.salary);
}

Since salary is private, you would/will have to declare this operator as a friend of the class for it to work.

You usually want to do one of these or the other. If implicit conversions make sense, then you probably want to support them on both operands. If they don't make sense, then you probably want to prohibit them entirely. The middle ground -- a + that can convert one operand but not the other rarely makes much sense.

Then again, I'd argue that supporting addition on employees doesn't make sense anyway. It would not be immediately obvious to me (or I think most readers) that adding two employees would yield a third employee object containing the sum of the two employees' salaries, with the rest of the data invalid. In fact, I'd argue that you probably shouldn't allow creation of such an invalid employee object at all.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

return type of overload operator + exhibit weird behaviour

From Dev

Overload function-call operator and return type

From Dev

Overload function-call operator and return type

From Dev

Return type of array access operator overload

From Dev

Java - ternary operator weird behaviour

From Dev

Javascript assignment operator weird behaviour

From Dev

Javascript assignment operator weird behaviour

From Dev

Can I overload 'operator new' with a different return type?

From Dev

Can I overload 'operator new' with a different return type?

From Dev

Overload operator < , == for a fraction type

From Dev

Unrelated deleted operator changes behaviour of overload resolution

From Dev

Ruby: overload operator behaviour for some cases only

From Dev

Overload by return type and arguments

From Dev

struct Equality operator Immediate Window weird behaviour

From Dev

struct Equality operator Immediate Window weird behaviour

From Dev

Extremely weird behaviour of return statement : Java

From Java

Error when overloading operator<<, "cannot overload functions distinguished by return type alone"

From Dev

how does operator overload resolution work based on return type in the following code of c++

From Java

Different behaviour of comma operator in C++ with return?

From Dev

Operator Overload for Template Class Auto Type Conversion

From Dev

Is there a way to create operator/function overload for container type

From Dev

Return type of assignment operator

From Dev

Operator "<<" overloading return type

From Dev

Return not working, getItemViewType behaviour is extremely weird - I cannot explain it

From Dev

Return not working, getItemViewType behaviour is extremely weird - I cannot explain it

From Dev

Cannot overload functions distinguished by return type alone

From Dev

The relationship of overload and method return type in Java?

From Dev

Overload operator []

From Dev

C++ operator overloading weird type conversion

Related Related

  1. 1

    return type of overload operator + exhibit weird behaviour

  2. 2

    Overload function-call operator and return type

  3. 3

    Overload function-call operator and return type

  4. 4

    Return type of array access operator overload

  5. 5

    Java - ternary operator weird behaviour

  6. 6

    Javascript assignment operator weird behaviour

  7. 7

    Javascript assignment operator weird behaviour

  8. 8

    Can I overload 'operator new' with a different return type?

  9. 9

    Can I overload 'operator new' with a different return type?

  10. 10

    Overload operator < , == for a fraction type

  11. 11

    Unrelated deleted operator changes behaviour of overload resolution

  12. 12

    Ruby: overload operator behaviour for some cases only

  13. 13

    Overload by return type and arguments

  14. 14

    struct Equality operator Immediate Window weird behaviour

  15. 15

    struct Equality operator Immediate Window weird behaviour

  16. 16

    Extremely weird behaviour of return statement : Java

  17. 17

    Error when overloading operator<<, "cannot overload functions distinguished by return type alone"

  18. 18

    how does operator overload resolution work based on return type in the following code of c++

  19. 19

    Different behaviour of comma operator in C++ with return?

  20. 20

    Operator Overload for Template Class Auto Type Conversion

  21. 21

    Is there a way to create operator/function overload for container type

  22. 22

    Return type of assignment operator

  23. 23

    Operator "<<" overloading return type

  24. 24

    Return not working, getItemViewType behaviour is extremely weird - I cannot explain it

  25. 25

    Return not working, getItemViewType behaviour is extremely weird - I cannot explain it

  26. 26

    Cannot overload functions distinguished by return type alone

  27. 27

    The relationship of overload and method return type in Java?

  28. 28

    Overload operator []

  29. 29

    C++ operator overloading weird type conversion

HotTag

Archive