overloading of operators is actually a special function overload, you must define a function and tell the C + + compiler to call this function when it encounters the operator of that overload. This function is called an operator overload function, typically a member function of a class.
Defines the general format of operator overloading functions:
Return value type class Name:: operator overloaded operator (parameter table)
{......}
Operator is a keyword that, together with overloaded operators, forms the name of a function. Because of the particularity of the function name, the C + + compiler can identify such functions.
Summary:
1. The function name of the operator overload function must be the keyword operator plus a valid operator. When the function is called, the right operand is the argument of the function.
2. When an overload of the operator is implemented with the member function of the class, the arguments of the operator overload function (when the binocular operator) are one or (when the single-mesh operator) are not. The left operand of an operator must be an object, because the overloaded operator is a member function of the object, and the right operand is the parameter of the function.
3. The single-mesh operator "+ +" and "--" have pre and post problems.
The front "+ +" format is:
return type class Name:: operator++ () {...}
The following "+ +" format is:
return type class Name:: operator++ (int) {...}
The parameter int in the post "+ +" is used only as a distinction and has no practical meaning, can give a variable name, or it can not give the variable name.
4. Only a very small number of operators in C + + do not allow overloading.
Overloaded operators have the following limitations
1). Non-Figment new operator
2). Cannot change the precedence, binding, and syntactic structure of the operator, and cannot change the number of operator operands
3). Operator overloading should not be used too much
4). Overloaded operator meaning must be clear, cannot have two semantics
The string operator overloads specific code as follows:
#include <iostream> #include <string>using namespace Std;class string; First declare ostream& operator<< (ostream &out, const String &s);//output stream istream& operator>> (istream &in, String &s); Input Stream class string//define String class {friend ostream& operator<< (ostream &out , const String &s);//friend friend istream& operator>> (IStream &in, String &s);p ublic:string (const c Har *str = null)//constructor {if (str = = null) {m_data = new char[1];m_data[0] = ' + ';} Else{m_data = new Char[strlen (str) +1];strcpy (M_DATA,STR);}} String (const string &s)//copy constructor {m_data = new Char[strlen (s.m_data) +1];strcpy (m_data,s.m_data) ;} string& operator= (const String &s)//operator overload {if (This! = &s) {free (m_data); m_data = new Char[strlen (s . m_data) +1];strcpy (m_data,s.m_data);} return *this;} ~string () destructor {delete []m_data;} String operator+ (const string &s)//s = S1 + S2{char *p = new Char[strlen (m_data) +strlen (s.m_data) +1]; strcpy (P,m_data); strcat (P,s.m_data); String tmp (P);d elete []p;return tmp;} String operator+= (const string &s)//s1 + = S2{char *p = m_data;p = new Char[strlen (m_data) +strlen (s.m_d ATA) +1];strcpy (p,m_data); strcat (P,s.m_data); String tmp (P);d elete []p;return tmp;} char& operator[] (int index)//overloaded subscript operator {return m_data[index];} BOOL operator== (String &s)//s1 = S2{if (strcmp (m_data,s.m_data) = = 0) {return true;} Else{return false;}} BOOL Operator!= (String &s)//s1! = S2{if (strcmp (m_data,s.m_data)! = 0) {return true;} Else{return false;}} BOOL Operator> (String &s)//s1 > S2{if (strcmp (M_data,s.m_data) > 0) {return true;} Else{return false;}} BOOL Operator<= (String &s)//s1 <= s2{if (sTRCMP (M_data,s.m_data) <= 0) {return true;} Else{return false;}} BOOL operator< (String &s)//s1 < S2{if (strcmp (M_data,s.m_data) < 0) {return true;} Else{return false;}} BOOL Operator>= (String &s)//s1 >= s2{if (strcmp (m_data,s.m_data) >= 0) {return true; }else{return false;}} Private:char *m_data; Private member};ostream& operator<< (ostream &out, const String &s) {Out<<s.m_data;return out;} istream& operator>> (IStream &in, String &s) {In>>s.m_data;return in;} void main ()//test function {String S1 ("Hello"); String S2 ("World"); String s3= s1 + s2; String S;s = S1 + = s2; cout<<s1[0]<<endl;cout<<s1<<endl;cout<<s3<<endl;cout<<s<<endl; String S4 ("HelloWorld"); String S5 ("HelloWorld"); String S6 ("Hello"); String S7 ("HI"); if (S4 = = S5) {cout<< "S4 and S5 equal!" <<enDL;} else{cout<< "S4 and S5 are not equal!" <<endl;} if (S4 > S7) {cout<< "S4 Greater than s7!" <<endl;} else if (S4 < S7) {cout<< "S4 Less than s7!" <<endl;} else {cout<< "S4 equals s7!" <<endl;} If (S4 >= S6) {cout<< "S4 large equals s6!" <<endl;} else if (S4 <= S6) {cout<< "S4 small equals s7!" <<endl;}}
I hope you can point out my shortcomings and thank you all.
String operator overloading