Basically it's defining the way two objects behave when an operator is used on them. You can create an instance of an object such as a string, for example - what should happen when somebody says:In the rules of English, there's no default action that you should take when somebody applies addition to a string, so in C++ you can overload the operator and make the two strings actually do something useful when you add them. Like joining them (this is actually what C++ already does, but it's an example of how operators are overloaded.PHP Code:string s("lol" + " internets");
).
Similarly, what should happen when you try to divide two strings? C++ doesn't define an action for string division, so you could choose whatever behaviour you like. You could return the lengths of the strings divided; you could return the first half of the first string matched up with the second half of the second string and vice versa; you could even do something like this:PHP Code:string string::operator+ (string first, string second)
{
// Some code here to join both strings together and return them
}
Then, if someone tried to divide strings:PHP Code:string string::operator/ (string first, string second)
{
return "That doesn't even make sense!";
}
PHP Code:string s1("lol");
string s2("internets");
string s3 = s1/s2;
// The string "s3" now contains the string "That doesn't even make sense



).
Reply With Quote