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

谁说我不能喝 提交于 2019-12-12 05:19:02

问题


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:


The correct prototype is the following:

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


来源:https://stackoverflow.com/questions/33550749/overloading-operator-in-c-how-do-you-pass-the-left-operand

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!