code_junkie

Should operator<< be implemented as a friend or as a member function?

Nov 11th, 2011
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.87 KB | None | 0 0
  1. friend bool operator<<(obj const& lhs, obj const& rhs);
  2.  
  3. ostream& operator<<(obj const& rhs);
  4.  
  5. friend ostream & operator<<(ostream &os, const Paragraph& p) {
  6. return os << p.to_str();
  7. }
  8.  
  9. ostream & operator<<(ostream &os) {
  10. return os << paragraph;
  11. }
  12.  
  13. Paragraph::to_str = (return paragraph)
  14.  
  15. #include <iostream>
  16.  
  17. class Paragraph
  18. {
  19. public:
  20. Paragraph(std::string const& init)
  21. :m_para(init)
  22. {}
  23.  
  24. std::string const& to_str() const
  25. {
  26. return m_para;
  27. }
  28.  
  29. bool operator==(Paragraph const& rhs) const
  30. {
  31. return m_para == rhs.m_para;
  32. }
  33. bool operator!=(Paragraph const& rhs) const
  34. {
  35. // Define != operator in terms of the == operator
  36. return !(this->operator==(rhs));
  37. }
  38. bool operator<(Paragraph const& rhs) const
  39. {
  40. return m_para < rhs.m_para;
  41. }
  42. private:
  43. friend std::ostream & operator<<(std::ostream &os, const Paragraph& p);
  44. std::string m_para;
  45. };
  46.  
  47. std::ostream & operator<<(std::ostream &os, const Paragraph& p)
  48. {
  49. return os << p.to_str();
  50. }
  51.  
  52.  
  53. int main()
  54. {
  55. Paragraph p("Plop");
  56. Paragraph q(p);
  57.  
  58. std::cout << p << std::endl << (p == q) << std::endl;
  59. }
  60.  
  61. ostream & operator<<(ostream &os) {
  62. return os << paragraph;
  63. }
  64.  
  65. // T << Paragraph
  66. T & operator << (T & p_oOutputStream, const Paragraph & p_oParagraph)
  67. {
  68. // do the insertion of p_oParagraph
  69. return p_oOutputStream ;
  70. }
  71.  
  72. // T >> Paragraph
  73. T & operator >> (T & p_oInputStream, const Paragraph & p_oParagraph)
  74. {
  75. // do the extraction of p_oParagraph
  76. return p_oInputStream ;
  77. }
  78.  
  79. // T << Paragraph
  80. T & T::operator << (const Paragraph & p_oParagraph)
  81. {
  82. // do the insertion of p_oParagraph
  83. return *this ;
  84. }
  85.  
  86. // T >> Paragraph
  87. T & T::operator >> (const Paragraph & p_oParagraph)
  88. {
  89. // do the extraction of p_oParagraph
  90. return *this ;
  91. }
  92.  
  93. // OUTPUT << Paragraph
  94. template <typename charT, typename traits>
  95. std::basic_ostream<charT,traits> & operator << (std::basic_ostream<charT,traits> & p_oOutputStream, const Paragraph & p_oParagraph)
  96. {
  97. // do the insertion of p_oParagraph
  98. return p_oOutputStream ;
  99. }
  100.  
  101. // INPUT >> Paragraph
  102. template <typename charT, typename traits>
  103. std::basic_istream<charT,traits> & operator >> (std::basic_istream<charT,traits> & p_oInputStream, const CMyObject & p_oParagraph)
  104. {
  105. // do the extract of p_oParagraph
  106. return p_oInputStream ;
  107. }
  108.  
  109. // OUTPUT << A
  110. std::ostream & operator << (std::ostream & p_oOutputStream, const Paragraph & p_oParagraph)
  111. {
  112. // do the insertion of p_oParagraph
  113. return p_oOutputStream ;
  114. }
  115.  
  116. // INPUT >> A
  117. std::istream & operator >> (std::istream & p_oInputStream, const Paragraph & p_oParagraph)
  118. {
  119. // do the extract of p_oParagraph
  120. return p_oInputStream ;
  121. }
  122.  
  123. bool operator<<(const obj&, const obj&);
  124.  
  125. ostream& operator<<(ostream&, const obj&);
Add Comment
Please, Sign In to add comment