Assume:
class Token
{
std::map<std::string, std::string> m_data;
public:
std::string& operator[](const char* c)
{return m_data[std::string(c)];}
};
int main()
{
// how to make this work:
Token t;
t["test"] = "Hello World";
t["test2"] = 12.345;
}
Looks like you need a runtime-generic holder that can store different
types. Here's a sketch of what these things might look like:
class Thing {
// abstract conversion and cast operators
Thing* clone() const; // polymorphic copy
};
class Double : public Thing {
// concrete definitions
};
class String : public Thing {
// blah
};
then use a wrapper/smart pointer class to hold the thing
class SmartPtr {
Thing* impl;
public:
// operator overloads , use clone() to copy
then use a map< std::string, SmartPtr<Thing> >
Fortunately, the work has already been done for you, boost
includes a nice version of this generic container (the subclasses
are parametrized ... a clever fusion of runtime polymorphism and
templates)
map<std::string, boost::any> t;
t["test"] = std::string("Hello World");
t["test2"] = 12.345;
double d = boost::any_cast<double> ( t["test2"] );
Cheers,