1.运算符重载
1.1定义:
对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型。内置数据类型表达式的重载运算符不能修改
2.加号运算符重载
可以通过类的成员函数实现,也可以通过全局函数实现
运算符重载,也可以实现函数重载,即可以实现两个自定义数据类型、也可以实现一个自定义数据类型和已有的数据类型
// 全局函数实现加号运算符重载
Person operator+(Person &p1, Person &p2) {
Person temp(0,0);
temp.m_A = p1.getM_A() + p2.getM_A();
temp.m_B = p1.getM_B() + p2.getM_B();
return temp;
}
//局部函数实现重载
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;
}
3.左移运算符重载
左移运算符重载友元可以实现自定义数据类型输出
左移运算符重载不能通过成员函数实现,只能通过全局函数实现,如要访问类中的私有变量,可以通过在
类中将全局函数声明为友员函数。
// 全局函数实现加号运算符重载
Person operator+(Person &p1, Person &p2) {
Person temp(0,0);
temp.m_A = p1.getM_A() + p2.getM_A();
temp.m_B = p1.getM_B() + p2.getM_B();
return temp;
}
4.递增运算符重载
函数的返回值不能作为重载函数的区别,可以通过用int代表占位符,用于区分前置递增和后置递增
重载后置++运算符
// 返回的是值,而不是引用
MyInteger operator++(int) { //int代表占位符,用于区分前置递增和后置递增
MyInteger temp = *this; // 先记录当时的结果
m_num++; // 后递增
return temp; // 返回本身
}
重载前置++运算符
//重载前置++运算符,返回引用是为了实现连续的递增运算
MyInteger& operator++() {
m_num++;
return *this; // 返回的类型是类的本身
}
5.赋值运算重载
知识点:
编译器至少给一个类添加4个函数
默认构造函数
默认析构函数
默认拷贝构造函数,对属性进行拷贝
赋值运算符operator==,做赋值运算操作也会出现深浅拷贝问题
编译器实现的是浅拷贝,赋值时,可能会存在不同对象的成员指针指向一个堆区地址,释放空间时将出现错误
需要通过重载==运算符进行深拷贝
6.关系运算符重载
//重载>运算符
bool operator>(House& h) {
if (m_price > h.m_price) {
return true;
}
else {
return false;
}
}
// 重载==运算符
bool operator==(House& h) {
if (m_price = h.m_price) {
return true;
}
else {
return false;
}
}
7.函数调用运算重载(仿函数)
函数调用运算符()也可以重载
由于重载后使用的方式非常像函数的调用,因此称为仿函数
仿函数没有固定写法,非常灵活
8.整体代码
#include<iostream>
#include<string>
using namespace std;
// 运算符重载
/*
定义:对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型
内置数据类型表达式的重载运算符不能修改
不能滥用重载运算符
*/
// 加号运算符重载
class Person {
friend ostream& operator<<(ostream& cout, Person& p);
public:
Person(int a,int b) { // 构造函数对私有变量进行赋值
this->m_A = a;
this->m_B = b;
}
int getM_A() {
return m_A;
}
int getM_B() {
return m_B;
}
int m_A;
int m_B;
//局部函数实现重载
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;
}
// 利用左移运算符重载左移运算符
// 通常不同成员函数来重载左移运算符,无法实现cout在左边,只能通过全局函数
/*void operator<<(cout) {
}*/
};
// 全局函数实现加号运算符重载
Person operator+(Person &p1, Person &p2) {
Person temp(0,0);
temp.m_A = p1.getM_A() + p2.getM_A();
temp.m_B = p1.getM_B() + p2.getM_B();
return temp;
}
// 运算符重载,也可以实现函数重载
Person operator+(Person& p, int num) {
Person temp(0,0);
temp.m_A = num + p.m_A;
temp.m_B = num + p.m_B;
return temp;
}
// 利用全局函数实现左移运算符
ostream& operator<<(ostream& cout, Person& p) {
cout << "m_A=" << p.m_A << " m_B=" << p.m_B;
return cout;
}
void test01() {
Person p1(10, 10);
Person p2(10, 10);
// 重载本质
//Person p3 = p1.operator+(p2);
Person p3 = p1 + p2;
cout << p3.getM_A() << endl;
cout << p3.getM_B() << endl;
Person p4 = p1 + 1000;
cout << p4.getM_A() << endl;
}
//-----------------------------------------------------------------
// 左移运算符重载
// 左移运算符配合友员可以实现自定义数据类型输出
void test02() {
Person p(10,10);
p.m_A = 10;
p.m_B = 10;
//cout << p.m_A << endl;
cout << p << " hello wordl" << endl; //链式输出
}
//--------------------------------------------------------------------
// 递增运算符的重载
class MyInteger {
friend ostream& operator<<(ostream& cout, MyInteger myinteger);
private:
int m_num;
public:
MyInteger(){
m_num = 0;
}
//重载后置++运算符 // 返回值不能作为重载函数的区别,int代表占位参数,用于区分前置递增和后置递增
// 返回的是值,而不是引用
MyInteger operator++(int) {
MyInteger temp = *this; // 先记录当时的结果
m_num++; // 后递增
return temp; //
}
//重载前置++运算符,返回引用是为了实现连续的递增运算
MyInteger& operator++() {
m_num++;
return *this; // 返回的类型是类的本生
}
};
// 前置递增重载运算符
void test03() {
MyInteger myint;
cout << myint << endl;
cout << ++myint << endl;
cout << ++(++myint) << endl;
//cout << ++myint << endl;
}
void test04() {
MyInteger myint;
cout << myint << endl;
//cout << myint++ << endl;
cout << myint++ << endl;
}
//--------------------------------------------------------------------------
// 赋值运算符重载
/*
编译器至少给一个类添加4个函数
默认构造函数
默认析构函数
默认拷贝构造函数,对属性进行拷贝
赋值运算符operator=,对属性进行值拷贝
如果类中有属性指向堆区,做赋值运算操作也会出现深浅拷贝问题
*/
class Tree {
public:
Tree(int h) {
height = new int(h);
}
int* height;
// 赋值运算符=重载
Tree& operator=(Tree &t) {
if (height != NULL) {
cout << "来到了这里" << endl;
*height = *t.height;
}
else {
height = new int(*t.height);
}
return *this; // 返回本身
}
// 析构函数释放内存
~Tree() {
if (height != NULL) {
delete height;
height = NULL;
}
}
};
void test05() {
Tree t1(10);
Tree t2(20);
Tree t3(30);
t3 = t2 = t1;
cout << "t1s height = " << *t1.height << endl;
cout << "t2s height = " << *t2.height << endl;
cout << "t3s height = " << *t3.height << endl;
}
//---------------------------------------------------------------------------
//关系运算符重载
/*
*/
class House {
public:
int m_price;
House(int price) {
m_price = price;
}
// 重载==运算符
bool operator==(House& h) {
if (m_price = h.m_price) {
return true;
}
else {
return false;
}
}
//重载>运算符
bool operator>(House& h) {
if (m_price > h.m_price) {
return true;
}
else {
return false;
}
}
};
void test06() {
House h1(10);
House h2(20);
if (h1 == h2) {
cout << "h1等于h2" << endl;
}
else {
cout << "h1不等于h2" << endl;
}
if (h1 > h2) {
cout << "h1的价格高于h2" << endl;
}
else {
cout << "h1的价格低于h2" << endl;
}
}
//------------------------------------------------------------------------------
// 函数调用运算重载
/*
* 函数调用运算符()也可以重载
* 由于重载后使用的方式非常像函数的调用,因此称为仿函数
* 仿函数没有固定写法,非常灵活
*/
class MyPrint {
public:
void operator()(string test) {
cout << test << endl;
}
};
class MyAdd {
public:
int operator()(int a, int b) {
return a + b;
}
};
void test07() {
MyPrint myPrint;
myPrint("hello world!"); // 非常类似函数调用,称为仿函数
MyAdd add;
int res = add(10, 10);
cout << "res=" << res << endl;
cout << MyAdd()(100, 100) << endl;
}
int main() {
//test01();
//test02();
//test03();
//test04();
//test05();
//test06();
test07();
system("pause");
return 0;
}