The title is probably misleading, but here is what I am trying to do... and of course it doesn't compile. Any idea why I cannot get a TokenString to auto-convert to an std::string or const char* that can be used for string assignment?
Code:
#include <string>
#include <cstdlib>
class TokenString
{
public:
TokenString(const std::string& str) : m_str(str) {}
operator std::string() const { return m_str; }
operator int() const { return atoi(m_str.c_str()); }
operator unsigned int() const { return atoi(m_str.c_str()); }
operator double() const { return strtod(m_str.c_str(), 0); }
operator const char*() const { return m_str.c_str(); }
private:
std::string m_str;
};
int main()
{
TokenString tk("foo");
// This is ok
std::string str1 = tk;
// This is ok
std::string str2;
str2 = "foo";
// This is NOT ok
std::string str3;
str3 = tk; <--- Will not compile!
}