A simple class with explicit conversion constructor.
class MyDouble {
double d;
public:
MyDouble() : d(0) {}
explicit MyDouble(double d_) : d(d_) {}
MyDouble & operator =(double d_) {
d = d_; return *this;
}
};
I add an assignment on purpose to make it can be assigned constructed from double
.
MyDouble a;
a = 1.1; // this works
MyDouble b = MyDouble(1.1); // this works
MyDouble c(1.1); // this works
MyDouble d = 1.1; // this does not work
I do not want implicit conversion, cause it will cause some other problems. But I still want direct assignment work, but it does not. Is there anyway to make the last statement MyDouble d = 1.1;
work without deleting the explicit
keyword.