わかりましたので、「弱い型付け」IEを持つクラスがあり、次のように定義されたさまざまな型を格納できます。
#include <string>
class myObject{
public:
bool isString;
std::string strVal;
bool isNumber;
double numVal;
bool isBoolean;
bool boolVal;
double operator= (const myObject &);
};
次のように代入演算子をオーバーロードしたいと思います。
double myObject::operator= (const myObject &right){
if(right.isNumber){
return right.numVal;
}else{
// Arbitrary Throw.
throw 5;
}
}
私がこれを行うことができるように:
int main(){
myObject obj;
obj.isNumber = true;
obj.numVal = 17.5;
//This is what I would like to do
double number = obj;
}
しかし、それを行うと、次のようになります。
error: cannot convert ‘myObject’ to ‘double’ in initialization
課題で。
私も試しました:
int main(){
myObject obj;
obj.isNumber = true;
obj.numVal = 17.5;
//This is what I would like to do
double number;
number = obj;
}
私が得たもの:
error: cannot convert ‘myObject’ to ‘double’ in assignment
足りないものはありますか?または、オーバーロードによってそのような変換を行うことは単に不可能ですかoperator=
?