fork download
  1. #include <map>
  2. #include <iostream>
  3. #include <string>
  4.  
  5. class BaseOption {
  6. public:
  7. virtual ~BaseOption() {}
  8. };
  9.  
  10. template<typename T>
  11. class Option : public BaseOption {
  12. public:
  13. explicit Option(T* v) {value = v;}
  14. ~Option() { delete value; }
  15. T* value;
  16. };
  17.  
  18. typedef std::map <std::string, BaseOption*> OptionMap;
  19.  
  20. OptionMap options_;
  21.  
  22. template<typename T>
  23. void Set(const std::string& option_, T* value)
  24. {
  25. BaseOption*& option_member = options_[option_];
  26. delete option_member;
  27. option_member = new Option<T>(value);
  28. }
  29.  
  30. template<typename T>
  31. void printAll()
  32. {
  33. std::cout << "#Elem in Map " << options_.size() << std::endl;
  34.  
  35. OptionMap::const_iterator it;
  36. for (it = options_.begin(); it != options_.end(); ++it)
  37. std::cout << "Arg: " << it->first << " Value: " << " Pointer: " << it->second <<
  38. " Pointer to value: " << static_cast<Option<T>*>(it->second)->value << std::endl;
  39. }
  40.  
  41. using namespace std;
  42.  
  43. using namespace std;
  44.  
  45. int main()
  46. {
  47. int tempval = 8000;
  48. cout << "Here is &tempVal " << &tempval << endl;
  49. Set<int>("IeAIf", &tempval);
  50. Set<int>("LcE8V", &tempval);
  51.  
  52. tempval = 16000;
  53. Set<int>("RVn1C", &tempval);
  54. Set<int>("XINa2", &tempval);
  55.  
  56. tempval = 1;
  57. Set<int>("st6Vz", &tempval);
  58. printAll<int>();
  59. }
  60.  
Success #stdin #stdout 0s 3232KB
stdin
Standard input is empty
stdout
Here is &tempVal 0xbfd1e1c8
#Elem in Map 5
Arg: IeAIf  Value:   Pointer: 0x9e08040  Pointer to value: 0xbfd1e1c8
Arg: LcE8V  Value:   Pointer: 0x9e08088  Pointer to value: 0xbfd1e1c8
Arg: RVn1C  Value:   Pointer: 0x9e080d0  Pointer to value: 0xbfd1e1c8
Arg: XINa2  Value:   Pointer: 0x9e08118  Pointer to value: 0xbfd1e1c8
Arg: st6Vz  Value:   Pointer: 0x9e08160  Pointer to value: 0xbfd1e1c8