Explain Operator Overloaders & This-> to me me.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • CodeTilYaDrop
    New Member
    • Aug 2007
    • 66

    #1

    Explain Operator Overloaders & This-> to me me.

    Could someone please help me understand some code. My teacher went over it last week, but I am lost on it still. Here is the code:

    BigNum BigNum::operato r+(const BigNum &b) {
    return this->plus(b);
    }

    I would like two things explained to me about this code. First, what is an operator overloader used for? Then, what is 'this->' for in the code above.

    Thanks - CTYD
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    An operator overload is to make your class easier to use. Say you have a Date class and you need to add a number of days to a Date. With an overload, you could have ciode like:
    [code=cpp]
    Date dt("02/192008");

    Date duedate = dt +30; //due in 30 days
    [/code]

    Without an overload you would need a function that has Date* and an int arguments to do the adding:
    [code=cpp]
    void AddToDate(Date* dt, Date* result, int incr)
    {
    //code to add incr to dt and out answer in result
    }
    [/code]
    Then you code in main() would look like:
    [code=cpp]
    Date dt("02/192008");

    Date duedate ;
    AddToDate(&dt, &duedate, 30);
    [/code]

    Which code looks easier an more intuitive to read??

    There's your answer.

    When you call a member function, the function needs to know which object it is supposed to use. The compiler passes the address of the correct object to use as an invisible first argument of a member function. That is the this pointer.

    Notice on the member functions that there is no explicit argument for the object to use.

    By coding this->, you identify a member variable or function. That is
    this->Name identifies Name as a member variable. Some programmers use m_Name for the member variable but this does not identify the variable as a member variable. It's just a variable named m_Name. Maybe it's a member variable, maybe it's not. You have to use this->Name to be crystal clear.

    Comment

    • CodeTilYaDrop
      New Member
      • Aug 2007
      • 66

      #3
      The operator overloader example does not register yet with me. I do understand my teacher's example a little more after looking at it though! This is good!

      Then, I really appreciate the information about the 'this->'. This makes it a little more clear to me. Thank U- CTYD

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Originally posted by CodeTilYaDrop
        The operator overloader example does not register yet with me.
        How about this:
        [code=c]
        char c = 'A';
        cout << c << endl; //you see A

        int i = c;
        cout << i << endl; //you see 65
        [/code]

        How does the << operator do two different things ???

        Answer: There are two << operator functins. One for char and one for int.
        [code=c]
        ostream& operator<<(ostr eam& os, int arg)
        {
        //shows 65
        }
        ostream& operator<<(ostr eam& os, char arg)
        {
        //Looks up thb 65 in a chart of symbols
        //and shows the symbol A
        }
        [/code]

        Now when you define your own classes you can overload the << operator so objects of you class can use cout:
        [code=c]
        char c = 'A';
        cout << c << endl; //you see A

        int i = c;
        cout << i << endl; //you see 65

        Date dt(2,20,2008);
        cout << dt << endl; //you see 02/20/2008
        [/code]

        Of course, you would have to write an operator<< function that can work with a Date:
        [code=cpp]
        ostream& operator<<(ostr eam& os, Date arg)
        {
        //takes the arg apart into month day and year
        //and displays the mon, day and year separated by slashes
        }
        [/code]

        Comment

        Working...