Okay so I have a class that has 'weak typing' I.E. it can store many different types defined as:
#include <string>
class myObject{
public:
bool isString;
std::string strVal;
bool isNumber;
double numVal;
bool isBoolean;
bool boolVal;
double operator= (const myObject &);
};
I would like to overload the assignment operator like this:
double myObject::operator= (const myObject &right){
if(right.isNumber){
return right.numVal;
}else{
// Arbitrary Throw.
throw 5;
}
}
So that I can do this:
int main(){
myObject obj;
obj.isNumber = true;
obj.numVal = 17.5;
//This is what I would like to do
double number = obj;
}
But when I do that, I get:
error: cannot convert ‘myObject’ to ‘double’ in initialization
At the assignment.
I have also tried:
int main(){
myObject obj;
obj.isNumber = true;
obj.numVal = 17.5;
//This is what I would like to do
double number;
number = obj;
}
To which I get:
error: cannot convert ‘myObject’ to ‘double’ in assignment
Is there something I am missing? or is it simply not possible to do a conversion like that by overloading operator=
.