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:
-
Date dt("02/192008");
-
-
Date duedate = dt +30; //due in 30 days
-
Without an overload you would need a function that has Date* and an int arguments to do the adding:
-
void AddToDate(Date* dt, Date* result, int incr)
-
{
-
//code to add incr to dt and out answer in result
-
}
-
Then you code in main() would look like:
-
Date dt("02/192008");
-
-
Date duedate ;
-
AddToDate(&dt, &duedate, 30);
-
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.