Try this with no C++ version dependency:
#include <iostream>
#include <map>
#include <cstdlib>
class Vote {
public:
enum DATA_TYPE
{
STRING,
INT,
DOUBLE
};
Vote(int value)
: m_type(DATA_TYPE::INT), m_ptr(new int(value)){}
Vote(std::string& value)
: m_type(DATA_TYPE::STRING), m_ptr(new std::string(value)){}
Vote(double value)
: m_type(DATA_TYPE::DOUBLE), m_ptr(new double(value)){}
int toInt() {
switch (m_type) {
case DATA_TYPE::INT:
return *(static_cast<int*>(m_ptr));
break;
case DATA_TYPE::DOUBLE:
return (int)*(static_cast<double*>(m_ptr));
break;
case DATA_TYPE::STRING:
return string2int(*(static_cast<std::string*>(m_ptr)));
}
}
double toDouble() {
switch (m_type) {
case DATA_TYPE::INT:
return (double)*(static_cast<int*>(m_ptr));
break;
case DATA_TYPE::DOUBLE:
return *(static_cast<double*>(m_ptr));
break;
case DATA_TYPE::STRING:
return string2double(*(static_cast<std::string*>(m_ptr)));
}
}
std::string toString() {
switch (m_type) {
case DATA_TYPE::INT:
return int2string(*(static_cast<int*>(m_ptr)));
break;
case DATA_TYPE::DOUBLE:
return double2string(*(static_cast<double*>(m_ptr)));
break;
case DATA_TYPE::STRING:
return *(static_cast<std::string*>(m_ptr));
}
}
int string2int(std::string str)
{
return atoi(str.c_str());
}
double string2double(std::string str)
{
return atof(str.c_str());
}
std::string int2string(int value)
{
char buffer[10];
itoa(value, buffer, 10);
return std::string(buffer);
}
std::string double2string(double value)
{
char buffer[24];
sprintf(buffer,"%f", value);
return std::string(buffer);
}
DATA_TYPE type()
{
return m_type;
}
private:
std::string m_strValue;
int m_intValue;
double m_douValue;
DATA_TYPE m_type;
void *m_ptr;
};
int main()
{
std::map<std::string, Vote> map;
map.insert(std::pair<std::string, Vote>("Data1", 2));
map.insert(std::pair<std::string, Vote>("Data2", 2.2));
std::string Data3Value("1.31232");
map.insert(std::pair<std::string, Vote>("Data3", Data3Value));
for (std::map<std::string, Vote>::iterator it_map = map.begin();
it_map != map.end();
it_map ++) {
Vote::DATA_TYPE type = (*it_map).second.type();
switch (type) {
case Vote::DATA_TYPE::INT:
std::cout << "Key: " << (*it_map).first << ", value: " << (*it_map).second.toInt() << std::endl;
std::cout << "Key: " << (*it_map).first << ", value: " << (*it_map).second.toDouble() << std::endl;
std::cout << "Key: " << (*it_map).first << ", value: " << (*it_map).second.toString() << std::endl;
break;
case Vote::DATA_TYPE::DOUBLE:
std::cout << "Key: " << (*it_map).first << ", value: " << (*it_map).second.toInt() << std::endl;
std::cout << "Key: " << (*it_map).first << ", value: " << (*it_map).second.toDouble() << std::endl;
std::cout << "Key: " << (*it_map).first << ", value: " << (*it_map).second.toString() << std::endl;
break;
case Vote::DATA_TYPE::STRING:
std::cout << "Key: " << (*it_map).first << ", value: " << (*it_map).second.toInt() << std::endl;
std::cout << "Key: " << (*it_map).first << ", value: " << (*it_map).second.toDouble() << std::endl;
std::cout << "Key: " << (*it_map).first << ", value: " << (*it_map).second.toString() << std::endl;
break;
}
}
return 0;
}
You can provide 3 type constructor and then you can accept 3 types to construct the objects, and then provide a void pointer, point to the data we "new" when constructing the object. When we call toInt(), toDouble(), toString() functions and use static_cast to cast pointer to the data type we want.
voteis always anunsigned int, or are you going to try to parse it and notice that it's entirely numeric and there's not decimal point?