I want to make an object that has many faces. By overloading typecasts, the object can show many faces. What I am wondering, however, is whether there is a way to have C++ assume the casts, rather than me haivng to type them in. Let me show you what I mean.
Code:
class CRYPTSTATE
{
public:
CRYPTSTATE(string name_in,int value_in) : name(name_in),value(value_in) {}
CRYPTSTATE() : value(-1) {}
operator string() {return name;}
operator int() {return value;}
bool operator == (CRYPTSTATE &other) {return (value == static_cast<int>(other));}
bool operator == (int &other) {return (value == other);}
bool operator == (string &other) {return (name == static_cast<string>(other));}
bool operator == (char* other)
{
string temp(other);
return (name == other);
}
private:
string name;
int value;
};
CRYPTSTATE CRYPT_FAIL("CRYPT_FAIL",0);
string afunction(bool def)
{
return (def) ? "This is the default output of afunction()" : CRYPT_FAIL;
}
Now, this code will not compile. because
Code:
return (def) ? "This is the default output of afunction()" : CRYPT_FAIL;
CRYPT_FAIL is not a string. If I static_cast<string> it, of course, it works. Is there a way the function can just assume the cast, since it's returning a string?