Advertisement
Guest User

Untitled

a guest
May 18th, 2014
344
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.03 KB | None | 0 0
  1. #include <iostream>
  2. #include <sstream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. class MyTypeA
  8. {
  9. public:
  10.     MyTypeA(int i) : _i(i) {};
  11.     virtual ~MyTypeA() {};
  12.  
  13.     string getStr() const { return to_string(_i); }
  14.  
  15. private:
  16.     int _i;
  17. };
  18.  
  19. class MyTypeB
  20. {
  21. public:
  22.     MyTypeB(int i) : _i(i) {};
  23.     virtual ~MyTypeB() {};
  24.  
  25.     string getStr() const { return to_string(_i); }
  26.  
  27. private:
  28.     int _i;
  29. };
  30.  
  31. class Message : public ostringstream
  32. {
  33. public:
  34.     Message() {};
  35.     virtual ~Message() {};
  36.  
  37.     Message& operator <<(const MyTypeA &a) {
  38.         return *this << a.getStr();
  39.     };
  40.  
  41.     Message& operator <<(const MyTypeB &b) {
  42.         return *this << b.getStr();
  43.     };
  44.  
  45.     template<class T>
  46.     Message& operator <<(const T &t)
  47.     {
  48.         (std::ostringstream&)(*this) << t;
  49.         return *this;
  50.     }
  51. };
  52.  
  53. int main()
  54. {
  55.     MyTypeA a(10);
  56.     MyTypeA b(-10);
  57.  
  58.     Message m;
  59.     m << a << "-" << b; // This works just fine!
  60.     m << a << "-" << b << endl; // Ba dumm tss
  61.  
  62.     return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement