运算符重载
概念:对已有的运算符进行重新定义,赋予其另一种功能,以适应不同的数据类型
加号运算符重载
作用:实现两个自定义数据类型相加的运算
通过成员函数重载+号:
#include <iostream>
using namespace std;
class person
{
public:
person operator+(person& p)
{
person temp;
temp.m_a = this->m_a + p.m_a;
temp.m_b = this->m_b + p.m_b;
return temp;
}
int m_a;
int m_b;
};
void test0905()
{
person p1;
p1.m_a = 10;
p1.m_b = 10;
person p2;
p2.m_a = 10;
p2.m_b = 10;
person p3 = p1 + p2;
cout << p3.m_a << endl;
cout << p3.m_b << endl;
}
int main()
{
test0905();
return 0;
}
成员函数本质调用
person p3 = p1.operator+(p2)
通过全局函数重载+号:
#include <iostream>
using namespace std;
class person
{
public:
int m_a;
int m_b;
};
person operator+(person& p1, person& p2)
{
person temp;
temp.m_a = p1.m_a + p2.m_a;
temp.m_b = p1.m_b + p2.m_b;
return temp;
}
void test0905()
{
person p1;
p1.m_a = 10;
p1.m_b = 10;
person p2;
p2.m_a = 10;
p2.m_b = 10;
person p3 = p1 + p2;
cout << p3.m_a << endl;
cout << p3.m_b << endl;
}
int main()
{
test0905();
return 0;
}
全局函数本质调用
person p3 = operator+(p1,p2);
左移运算符重载
通常不会利用成员函数重载<<运算符,因为无法实现cout在左侧
全局函数重载<<:
#include <iostream>
using namespace std;
class person
{
friend ostream& operator<<(ostream& cout, person& p);
public:
person(int a, int b)
{
m_a = a;
m_b = b;
}
private:
int m_a;
int m_b;
};
ostream & operator<<(ostream &cout,person &p)
{
cout << "m_a = " << p.m_a << " m_b = " << p.m_b;
return cout;
}
void test0905_1()
{
person p1(10,10);
cout << p1<<endl;
}
int main()
{
test0905_1();
return 0;
}
递增运算符重载
#include <iostream>
using namespace std;
class myinteger
{
friend ostream& operator<<(ostream& cout, const myinteger& myint);
public:
myinteger()
{
m_num = 0;
}
//重载前置++运算符
myinteger & operator++()
{
m_num++;
return *this;
}
//重载后置++运算符
myinteger operator++(int)
{
static myinteger temp = *this;
m_num++;
return temp;
}
private:
int m_num;
};
ostream& operator<<(ostream& cout, const myinteger& myint)
{
cout << myint.m_num;
return cout;
}
void my_test01()
{
myinteger myint01;
myinteger myint02;
cout << ++myint01 << endl;
cout << myint01 << endl;
cout << myint02++ << endl;
cout << myint02 << endl;
}
int main()
{
my_test01();
return 0;
}
赋值运算符重载
c++编译器至少给一个类添加四个函数
1.默认构造函数(无参,函数体为空)
2.默认析构函数(无参,函数体为空)
3.默认拷贝构造函数,对属性进行值拷贝
4.赋值运算符operator=,对属性进行值拷贝
#include <iostream>
using namespace std;
class person
{
public:
person(int age)
{
m_age = new int(age);
}
~person()
{
if (m_age != NULL)
{
delete m_age;
m_age = NULL;
}
}
person & operator=(person& p)
{
if (m_age != NULL)
{
delete m_age;
m_age = NULL;
}
new int(*p.m_age);
m_age = new int(*p.m_age);
return *this;
}
int* m_age;
};
void test1422()
{
person p1(18);
person p2(20);
person p3(30);
p2 = p1 = p3;
cout << "p1年龄为:" << *p1.m_age << endl;
cout << "p2年龄为:" << *p2.m_age << endl;
cout << "p3年龄为:" << *p3.m_age << endl;
}
int main()
{
test1422();
return 0;
}
关系运算符重载
#include <iostream>
#include <string>
using namespace std;
class person
{
public:
person(string name, int age)
{
m_name = name;
m_age = age;
}
bool operator==(person &p)
{
if (this->m_name == p.m_name && this->m_age == p.m_age)
{
return true;
}
return false;
}
bool operator!=(person& p)
{
if (this->m_name == p.m_name && this->m_age == p.m_age)
{
return false;
}
return true;
}
string m_name;
int m_age;
};
void test()
{
person p1("tom", 30);
person p2("tom", 20);
if (p1 == p2)
{
cout << "二者相等" << endl;
}
else cout << "二者不等" << endl;
if (p1 != p2)
{
cout << "二者不等" << endl;
}
else cout << "二者相等" << endl;
}
int main()
{
test();
return 0;
}
函数调用运算符重载
函数调用运算符()也可以重载
用于重载后使用的方式非常像函数的调用 ,因此也被成为仿函数
仿函数没有固定写法,非常灵活
#include <iostream>
#include <string>
using namespace std;
class myprint
{
public:
void operator()(string test)
{
cout << test << endl;
}
};
void test()
{
myprint myprint;
myprint("hello world");
}
int main()
{
test();
return 0;
}