Search
 
SCRIPT & CODE EXAMPLE
 

CPP

why the << operator is friend

#include <iostream>

class Paragraph
{
    public:
        explicit Paragraph(std::string const& init)
            :m_para(init)
        {}

        std::string const&  to_str() const
        {
            return m_para;
        }

        bool operator==(Paragraph const& rhs) const
        {
            return m_para == rhs.m_para;
        }
        bool operator!=(Paragraph const& rhs) const
        {
            // Define != operator in terms of the == operator
            return !(this->operator==(rhs));
        }
        bool operator<(Paragraph const& rhs) const
        {
            return  m_para < rhs.m_para;
        }
    private:
        friend std::ostream & operator<<(std::ostream &os, const Paragraph& p);
        std::string     m_para;
};

std::ostream & operator<<(std::ostream &os, const Paragraph& p)
{
    return os << p.to_str();
}


int main()
{
    Paragraph   p("Plop");
    Paragraph   q(p);

    std::cout << p << std::endl << (p == q) << std::endl;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: faster solutions 
Cpp :: how to run cpp in visual studio 
Cpp :: c++ file handiling 
Cpp :: arduino bleutooth module hc-05 with led 
Cpp :: parking charge system project c++ 
Cpp :: new expression 
Cpp :: full pyramid in c++ 
Cpp :: c create 1 bit value 
Cpp :: variable modulus 5 meaning in c++ 
Cpp :: cap phat dong mang 2 chieu trong c++ 
Cpp :: forkortelse for intet 
Cpp :: texorpdfstring math in title latex 
Cpp :: PascalName seperate strings 
Cpp :: open url from dev cpp 
Cpp :: linq select where string equals "String" 
Cpp :: private access specifier in c++ program 
Cpp :: dinamic 
Cpp :: cpp stacks 
Cpp :: c++ int max value 
Cpp :: product of array in cpp 
Cpp :: Corong_ExerciseNo3(1) 
Cpp :: set(W) 
Cpp :: 400 watt hour per kg 
Cpp :: c++ cyclic barrier 
Cpp :: easy way to learn file handling in c++ array 
Cpp :: find the number of digits of a given integer n . 
Cpp :: c+ - Dormir en millisecondes 
Cpp :: query for rmq using sqrt decomposition 
Cpp :: C++ Dynamic allocation failing 
Cpp :: Int main ( ) { int i,n; cinn; i=n; while(i=1) { i=i+5; i=i-6; } } 
ADD CONTENT
Topic
Content
Source link
Name
8+9 =