Advertisement
Guest User

Overloading on return type for map access

a guest
Feb 9th, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.97 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <map>
  4.  
  5. struct Properties {
  6.     Properties();
  7.     Properties(std::string key) : key_(key) {};
  8.     std::map<std::string, std::string> props = { {"string", "string"}, {"int", "46"}, {"bool", "false"} };
  9.     operator std::string() { return getString(); };
  10.     operator bool() { return getBool(); };
  11.     operator int() { return getInt(); };
  12. private:
  13.     std::string key_;
  14.     int getInt() { if (props[key_].find_first_of("0123456789") != std::string::npos) return std::stoi(props[key_]); };
  15.     bool getBool() { if (props[key_].find("true") != std::string::npos || props[key_].find("false") != std::string::npos) return (props[key_].find("true") != std::string::npos); };
  16.     std::string getString() { return props[key_]; };
  17. };
  18.  
  19.  
  20. int main() {
  21.     int integer = Properties("int");
  22.     std::string text = Properties("string");
  23.     bool boolean = Properties("bool");
  24.     std::cout << "int, string, bool: " << integer << ' ' << text << ' ' << boolean << '\n';
  25.     return 0;
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement