重载运算符=两倍 [英] Overloading operator= for double

查看:80
本文介绍了重载运算符=两倍的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可能重载= double类型的运算符?

Is it possible to overload = operator of type double?

我有以下内容:

double operator=(double a, Length b) {
    return a = (b.getInches()/12+b.getFeet())*3.2808*0.9144;
}

它引发以下错误:

'double operator=(double, Length)' must be a nonstatic member function

我在做什么错了?

推荐答案

您不能重载诸如 double 之类的内置(整数或浮点)类型的运算符,也不能全局重载= 运算符,用于 any 类型. = 运算符只能作为类成员函数重载.

You cannot overload operators for builtin (integral or floating point) types like double, and also you cannot globally overload the = operator for any type. The = operator can only be overloaded as a class member function.

另请参见:我可以定义一个运算符重载来与内置/固有/原始类型?

这篇关于重载运算符=两倍的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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