For single
argument constructors (that are not copy constructors) or constructors
that the compiler may use for conversion, declare the constructor as
explicit. Instead of using implicit type conversion operators that the
compiler may use unexpectedly, use custom methods explicitly.
class MyClass {
public:
explicit MyClass(int param, int param2=0);
double toDouble() const;
bool operator==(const MyClass& left, const MyClass& right);
private:
double num;
};
int main(int argc, char* argv[]){
MyClass a;
int b[] = {0,1,2};
for(int i=0; i<3; i++)
if( a.toDouble() == b[i] ){
}
}
}
|
|