urgent help with operator overloading

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ambr
    New Member
    • Oct 2006
    • 4

    #1

    urgent help with operator overloading

    need to know how to write definition to overload relational operators as a non member functions in c++ for e.g
    friend bool operator ==(const dataType&,const dataType&)
    friend bool operator <=(const dataType&,const dataType&

    these are prototypes how do iwrite the definitionsof these two.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Well that all rather depends on what dataType is and what logic you are using when defining the comparison

    for instance taking the structure

    Code:
    class myType
    {
    friend bool operator ==(const myType &op1, const myType &op2);
    
    
    proctected:
        char *m_Data;
        int size;
    
    public:
        myType();
        ~myType();
    }
    are 2 instances of myTye equal when m_Data point to the same location or are 2 instances of myType equal when the data pointed to by the 2 m_Data members is the same?

    By the way I don't think those friend keywords are required in the declarations.

    A comparison operator for this type could be

    Code:
    bool operator ==(const myType &op1, const myType &op2)
    {
        if (op1.size != op2.size)
            return false;
    
        return (memcmp(op1.m_data, op2.m_data, op1.size) == 0);
    }

    Comment

    Working...