Overloading += operator in C++ - How do you pass t

2019-09-08 09:24发布

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?

1条回答
相关推荐>>
2楼-- · 2019-09-08 10:02

The correct prototype is the following:

double& operator+=(double& value, const myClass& obj)
查看更多
登录 后发表回答