struct Foo {
int val;
Foo() : val(-1) {}
explicit Foo(int val_) : val(val_) {}
Foo& operator=(int val_) { val = val_; return *this; }
operator int () const { return val; }
};
int main()
{
Foo foo = 1; // error
Foo foo2;
foo2 = 2; // works fine
return 0;
}
error: conversion from 'int' to non-scalar type 'Foo' requested
Foo foo = 1;
The code should be self-explanatory. I would like to understand why the direct assignment is illegal when a suitable assignment operator has been defined.