过载运算符的返回类型+表现出奇怪的行为 [英] return type of overload operator + exhibit weird behaviour

查看:88
本文介绍了过载运算符的返回类型+表现出奇怪的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个班级雇员

#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;
}

我使运算符+过载,因此它加了2个雇员对象的薪水.重载的+运算符的返回类型为double.

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.

这是我的问题

1)为什么当我重载了运算符+以返回double而不是employee时,employee C = A + B为何起作用,应该不会出现编译器错误?

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)实际发生了什么?

2) what is actually happening???

推荐答案

当您重载operator+以返回双精度时,编译器将查看是否可以将double转换为employee对象.由于您有一个使用int的ctor,因此它将从double隐式转换为int,然后使用您的ctor将int转换为employee.

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.

与此同时,这没有多大意义.现在,您已经将employee的薪金成员定义为double,但是只允许用户指定int对其进行初始化.您可能希望允许使用double对其进行初始化.

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.

就目前而言,您的operator+也具有非对称行为:它可以在右侧操作数上进行隐式转换,但不能在左侧进行隐式转换,因此a + 1可以工作,但1 + a不能.

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.

您可以通过将转化构造函数(即所有可以通过单个参数调用的转化构造函数)explicit消除,以消除所有隐式转化.相反,您可以通过将重载实现为自由函数来允许在左或右操作数上进行隐式转换:

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);
}

由于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.

再说一次,我认为支持员工加薪毫无意义.对我(或我认为大多数读者)来说,立即增加两个employee不会产生一个包含两个employee薪金之和的第三个employee对象,其余数据无效.实际上,我认为您可能根本不应该允许创建这样一个无效的employee对象.

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.

这篇关于过载运算符的返回类型+表现出奇怪的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆