Guest User

Untitled

a guest
Mar 22nd, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. printf("%s", obj) //prints "some data"
  2.  
  3. #include <cstdio>
  4. #include <list>
  5. #include <string>
  6.  
  7.  
  8. class Example{
  9. public:
  10. operator int() const{
  11. return 5;
  12. }
  13. operator const char*() const{
  14. return m_text;
  15. }
  16. operator std::list<std::string>() const{
  17. return m_lines;
  18. }
  19. private:
  20. const char* m_text = "5";
  21. const std::list<std::string> m_lines = {"1", "2", "3"};
  22. };
  23.  
  24. void func(int d){
  25. std::printf("%dn", d);
  26. }
  27.  
  28. /*
  29. А вот так уже нельзя, неоднозначность, компилятор не будет знать что вы от него хотите
  30. void func(const std::list<std::string>& l){
  31. for (const auto& line:l){
  32. printf("%sn", line.c_str());
  33. }
  34. }
  35. */
  36.  
  37. void funcList(const std::list<std::string>& l){
  38. for (const auto& line:l){
  39. printf("%sn", line.c_str());
  40. }
  41. }
  42.  
  43. int main()
  44. {
  45. Example e;
  46. std::printf("%dn", (int)e);
  47. std::printf("%sn", (const char*)e);
  48. func(e);
  49. funcList(e);
  50. return 0;
  51. }
Add Comment
Please, Sign In to add comment