Beispiel

class MyClass {
public:
MyClass(int param, int param2=0);
operator double() 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 == b[i] ){
//b[i] implicitly converted to MyClass
}
}
}

Lösung
Bei Einzelargumentkonstruktoren, die keine Kopierkonstruktoren sind, oder bei Konstruktoren, die vom Compiler für die Konvertierung verwendet werden, deklarieren Sie den Konstruktor als expliziten Konstruktor. Setzen Sie explizit angepasste Methoden anstelle von Operatoren für eine implizite Typumsetzung ein, die möglicherweise vom Compiler versehentlich verwendet werden.

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] ){
//a explicitly converted to double
}
}
}