Advertisement
Guest User

Untitled

a guest
May 18th, 2014
772
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.11 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.  
  38.     ostream& operator <<(ostream &os, const MyTypeA &a) {
  39.         return os << a.getStr();
  40.     };
  41.  
  42.     ostream& operator <<(ostream &os, const MyTypeB &b) {
  43.         return os << b.getStr();
  44.     };
  45.      
  46.     int main()
  47.     {
  48.         MyTypeA a(10);
  49.         MyTypeA b(-10);
  50.      
  51.         Message m;
  52.         m << a << "-" << b; // This works just fine!
  53.         m << a << "-" << b << endl; // Ba dumm tss
  54.      
  55.         return 0;
  56.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement