简体   繁体   中英

Overloading += operator in C++ - How do you pass the left operand?

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

myClass myobject();

double mydouble = 10000;

mydouble += myobject;

My operator:

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM