fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. struct TheMainClass
  5. {
  6. struct AssignmentProxy
  7. {
  8. std::string name;
  9. TheMainClass* main;
  10.  
  11. AssignmentProxy(std::string const& n, TheMainClass* m)
  12. : name(n), main(m)
  13. {}
  14.  
  15. TheMainClass& operator=(std::string const& s)
  16. {
  17. main->addString(name, s);
  18. return *main;
  19. }
  20.  
  21. TheMainClass& operator=(int i)
  22. {
  23. main->addInteger(name, i);
  24. return *main;
  25. }
  26. };
  27.  
  28. AssignmentProxy operator[](std::string const& name)
  29. {
  30. return AssignmentProxy(name, this);
  31. }
  32.  
  33. void addString(std::string const& name, std::string const& str)
  34. {
  35. std::cout << "Adding string " << name << " with value \"" << str << "\"\n";
  36. }
  37.  
  38. void addInteger(std::string const& name, int i)
  39. {
  40. std::cout << "Adding integer " << name << " with value " << i << "\n";
  41. }
  42. };
  43.  
  44. int main(int argc __attribute__((unused)), char *argv[] __attribute__((unused)))
  45. {
  46. TheMainClass builder;
  47. builder[ "string_value" ] = "Hello";
  48. builder[ "int_value" ] = 5;
  49. builder[ "another_string" ] = "Thank you";
  50. }
  51.  
Success #stdin #stdout 0s 3456KB
stdin
Standard input is empty
stdout
Adding string string_value with value "Hello"
Adding integer int_value with value 5
Adding string another_string with value "Thank you"