The concept of operator overloading
Overloading in C + + can extend the function of operators
Operators are overloaded in the form of functions
Nature: extending the function of operators with special functions
Define special functions through the operator keyword * * overload operators
Syntax:
// Sign is a predefined operator in the system
Type operator Sign(const Type& p1, const Type& p2)
{
Type ret;
return ret;
}
-
You can define an operator overloaded function as a member function of a class
One parameter less than global operator overloaded function (left operand)
Compiler takes precedence in finding operator overloaded functions in member functions
-
Considerations for operator overloading
Operator overloading cannot change the priority of the original operator
Operator overload cannot change the number of operands
Operator overloading cannot change the original semantics of operators
Array operator ([]) overload
Array operators are built-in operators in C/C + +
-
The original meaning of array operator is array access and pointer operation
- a[n] <==> (a + n) <==> (n + a) <==> n[a]
-
Overloading array access operators enables objects to simulate array behavior
Can only be overloaded by a class's member function
Overloaded functions can and can only use one parameter
Multiple overloaded functions with different parameters can be defined
/*
Test code
*/
#include <iostream>
#include <string>
using namespace std;
class Test
{
int a[5];
public:
int& operator [] (int i)
{
return a[i];
}
int& operator [] (const string& s)
{
if( s == "1st" )
{
return a[0];
}
else if( s == "2nd" )
{
return a[1];
}
else if( s == "3rd" )
{
return a[2];
}
else if( s == "4th" )
{
return a[3];
}
else if( s == "5th" )
{
return a[4];
}
return a[0];
}
int length()
{
return 5;
}
};
int main()
{
Test t;
for(int i=0; i<t.length(); i++)
{
t[i] = i;
}
for(int i=0; i<t.length(); i++)
{
cout << t[i] << endl;
}
cout << endl;
cout << t["5th"] << endl;
cout << t["4th"] << endl;
cout << t["3rd"] << endl;
cout << t["2nd"] << endl;
cout << t["1st"] << endl;
return 0;
}
Operation result
0 1 2 3 4 4 3 2 1 0
Overloaded assignment operator (=)
C + + specifies that the assignment operator (=) can only be overloaded as a member function
The compiler overloads the assignment operator by default for each class
Default assignment operator only completes shallow copy
The assignment operator must be overloaded when a deep copy is required
Overloaded assignment operators have the same meaning as copy constructors
/*
Test code
*/
#include <iostream>
using namespace std;
class Test
{
private:
int* m_pointer;
public:
Test()
{
m_pointer = NULL;
}
Test(int i)
{
m_pointer = new int(i);
}
//User-Defined Copy Constructor
Test(const Test& obj)
{
m_pointer = new int(*obj.m_pointer);
}
// Assignment operator overload
Test& operator = (const Test& obj)
{
if(this != &obj)
{
delete m_pointer;
m_pointer = new int(*obj.m_pointer)
}
retutn *this;
}
void print()
{
cout << "m_pointer = " << m_pointer << endl;
}
~Test()
{
delete m_pointer;
}
};
int main()
{
Test t1(1);
Test t2;
t2 = t1;
t1.print();
t2.print();
return 0;
}
Operation result
m_pointer = 0x3810e8
m_pointer = 0x381108