Home > Mobile >  why here 2 cannot implicitly convert to MyClass object?
why here 2 cannot implicitly convert to MyClass object?

Time:06-21

class MyClass {
public:
    MyClass(int){}

};
MyClass operator  (MyClass& lOperand, MyClass& rOperand) {
    return lOperand;
}

int main() {
    MyClass obj1(0);
    obj1 = obj1   2;  // 1
}

Here compiler says no match function of operator in line 1. But I think 2 can be converted to a MyClass object as the constructor of MyClass is not explicit. If I take & away from the parameter, then it is OK.

CodePudding user response:

I think 2 can be converted to a MyClass object as the constructor of MyClass is not explicit.

The problem is that after the conversion from int to MyClass we will have a prvalue of type MyClass which cannot be bound to an lvalue reference to non-const MyClass.

Basically, an lvalue reference to nonconst type cannot be bound to an rvalue. But an lvalue reference to a const type can be bound to an rvalue. For example,

int &ref = 4; //not valid
const int &r = 4; //valid

To solve this, you can add a low-level const to the parameters as shown below:

//----------------------------------------- vvvvv----------------------->low level const added
MyClass operator  (const MyClass& lOperand, const MyClass& rOperand) {
    return lOperand;
}

Working demo

CodePudding user response:

When you write

MyClass operator  (MyClass& lOperand, MyClass& rOperand)

you are signaling that you are modifying the 2 arguments, because otherwise you surely would have used const, right?

But if the integer literal 2 is converted to MyClass you have a temporary object that will be destroyed at the end of the function call. Any modifications made to it will be lost.

Surely if you are going to modify arguments then you want those modifications to persist. So C in all its wisdom has decreed that a prvalue, which you get from the conversion from int to MyClass, can not be bound to an *lvalue reference to non-const MyClass. That way you don't loose your precious modifications.

But really operator should not be modifying it's arguments, declare them as const and all will be well.

  •  Tags:  
  • c
  • Related