重载+ = C ++中的运算符 - 如何传递左操作数? [英] Overloading += operator in C++ - How do you pass the left operand?

查看:174
本文介绍了重载+ = C ++中的运算符 - 如何传递左操作数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要创建一个接受双重'参数'的运算符。

I need to create an operator that accepts a double 'parameter'.

myClass myobject();

double mydouble = 10000;

mydouble += myobject;

我的操作员:

double operator+=(double value, const myclass& object)
{
    value += object.value;           
    return  value;
}

参数值被传递给运算符+ =为零, mydouble被初始化为10000.

The parameter value is being passed to the operator += as zero, even though mydouble is initialized to 10000.

如何创建一个可以接受左操作数作为参数的操作符?

How do you create an operator that can accept the left operand as a parameter?

推荐答案

正确的原型如下:

double& operator+=(double& value, const myClass& obj)

这篇关于重载+ = C ++中的运算符 - 如何传递左操作数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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