几种常见的运算符重载
1、加号运算符重载
#include <iostream>
using namespace std;
class Person
{
public:
Person() {};
Person(int a, int b)
{
this->m_A = a;
this->m_B = b;
}
Person operator+(const Person& p)
{
Person tmp;
tmp.m_A = this->m_A + p.m_A;
tmp.m_B = this->m_B + p.m_B;
return tmp;
}
Person operator+(const int val)
{
Person tmp;
tmp.m_A = this->m_A + val;
tmp.m_B = this->m_B + val;
return tmp;
}
public:
int m_A;
int m_B;
};
void test()
{
Person p1(10, 10);
Person p2(20, 20);
Person p3 = p1 + p2;
cout << "p3.m_A = " << p3.m_A << " p3.m_B = " << p3.m_B << endl;
Person p4 = p1 + 30;
cout << "p4.m_A = " << p4.m_A << " p4.m_B = " << p4.m_B << endl;
}
int main()
{
test();
system("pause");
return 0;
}
2、左移运算符重载
#include <iostream>
using namespace std;
class Person
{
friend ostream& operator<< (ostream& out, const Person& p);
public:
Person(int a, int b)
{
this->m_A = a;
this->m_B = b;
}
private:
int m_A;
int m_B;
};
ostream& operator<<(ostream& out, const Person& p)
{
out << "m_A = " << p.m_A << " m_B = " << p.m_B << endl;
return out;
}
void test()
{
Person p1(10, 20);
cout << p1 << "hello world" << endl;
}
int main()
{
test();
system("pause");
return 0;
}
3、递增运算符重载
#include <iostream>
using namespace std;
class MyInteger
{
friend ostream& operator<<(ostream& out, const MyInteger& myint);
public:
MyInteger()
{
m_Num = 0;
}
MyInteger& operator++()
{
this->m_Num++;
return *this;
}
MyInteger operator++(int)
{
MyInteger temp;
temp = *this;
this->m_Num++;
return temp;
}
private:
int m_Num;
};
ostream& operator<< (ostream& out, const MyInteger& myint)
{
out << myint.m_Num;
return out;
}
void test01()
{
MyInteger myInt;
cout << ++myInt << endl;
cout << myInt << endl;
}
void test02()
{
MyInteger myInt;
cout << myInt++ << endl;
cout << myInt << endl;
}
int main()
{
test01();
test02();
system("pause");
return 0;
}
4、赋值运算符重载
#include <iostream>
using namespace std;
class Person
{
public:
Person(int age)
{
m_Age = new int(age);
}
Person& operator=(Person& p)
{
if (m_Age != NULL) {
delete m_Age;
m_Age = NULL;
}
m_Age = new int(*p.m_Age);
return *this;
}
~Person()
{
if (m_Age == NULL) {
delete m_Age;
m_Age = NULL;
}
}
int* m_Age;
};
void test01()
{
Person p1(10);
Person p2(20);
Person p3(30);
p3 = p2 = p1;
cout << "*p1.m_Age = " << *p1.m_Age << endl;
cout << "*p2.m_Age = " << *p2.m_Age << endl;
cout << "*p3.m_Age = " << *p3.m_Age << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
5、关系运算符重载
#include <iostream>
using namespace std;
class Person
{
public:
Person(string name, int age)
{
this->m_Name = name;
this->m_Age = age;
}
bool operator==(Person& p)
{
if (this->m_Name == p.m_Name && this->m_Age == p.m_Age) {
return true;
}
else {
return false;
}
}
bool operator!=(Person& p)
{
if (this->m_Name == p.m_Name && this->m_Age == p.m_Age) {
return false;
}
else {
return true;
}
}
string m_Name;
int m_Age;
};
void test01()
{
Person a("孙悟空", 10);
Person b("孙悟空", 20);
if (a == b){
cout << "a和b相等" << endl;
}
else {
cout << "a和b不相等" << endl;
}
if (a != b) {
cout << "a和b不相等" << endl;
}
else {
cout << "a和b相等" << endl;
}
}
int main()
{
test01();
system("pause");
return 0;
}
6、函数调用运算符重载
#include <iostream>
using namespace std;
class MyPrint
{
public:
void operator()(string text)
{
cout << text << endl;
}
};
void test01()
{
MyPrint myFunc;
myFunc("hello world");
}
class MyAdd
{
public:
int operator()(int val1, int val2)
{
return val1 + val2;
}
};
void test02()
{
MyAdd add;
int ret = add(10, 10);
cout << "ret = " << ret << endl;
cout << "MyAdd()(100, 100) = " << MyAdd()(100, 100) << endl;
}
int main()
{
test01();
test02();
system("pause");
return 0;
}