文章内容转载自黑马程序员C++核心编程讲义,如有侵权,请联系作者删除
4.5 运算符重载
运算符重载概念:对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型
4.5.1 加号运算符重载
作用:实现两个自定义数据类型相加的运算
class Person { public: Person() {}; Person(int a, int b) { this->m_A = a; this->m_B = b; } Person operator+(const Person& p) { Person temp; temp.m_A = this->m_A + p.m_A; temp.m_B = this->m_B + p.m_B; return temp; }
public: int m_A; int m_B; };
Person operator+(const Person& p2, int val) { Person temp; temp.m_A = p2.m_A + val; temp.m_B = p2.m_B + val; return temp; }
void test() {
Person p1(10, 10); Person p2(20, 20);
Person p3 = p2 + p1; cout << "mA:" << p3.m_A << " mB:" << p3.m_B << endl;
Person p4 = p3 + 10; cout << "mA:" << p4.m_A << " mB:" << p4.m_B << endl;
}
int main() {
test();
system("pause");
return 0; }
|
自己写的:
#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; };
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; }
Person operator+(Person &p1,int num) { Person temp; temp.m_a = p1.m_a + num; temp.m_b = p1.m_b + num; return temp; }
void test01() { Person p1; p1.m_a = 10; p1.m_b = 10;
Person p2; p2.m_a = 10; p2.m_b = 10;
Person p3 = operator+(p1, p2);
Person p4 = p1 + 100; cout << p3.m_a << endl; cout << p3.m_b << endl;
cout << p4.m_a << endl; cout << p4.m_b << endl; }
int main() { test01(); return 0; }
|
总结1:对于内置的数据类型的表达式的的运算符是不可能改变的
总结2:不要滥用运算符重载
4.5.2 左移运算符重载
作用:可以输出自定义数据类型
class Person { friend ostream& operator<<(ostream& out, 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, Person& p) { out << "a:" << p.m_A << " b:" << p.m_B; return out; }
void test() {
Person p1(10, 20);
cout << p1 << "hello world" << endl; }
int main() {
test();
system("pause");
return 0; }
|
自己写的:
#include<iostream> using namespace std; class Person { friend ostream& operator<<(ostream& out, Person& p); friend ostream& operator<<(ostream& out, Person& p);
public: Person(int a, int b) { m_a = a; m_b = b; }
private: int m_a; int m_b; };
ostream& operator<<(ostream& out, Person& p) { out << "m_a=" << p.m_a << " " << "m_b=" << p.m_b; return out; } void test01() {
Person p(10, 10);
cout << p << endl; }
void test02() {
;
Person p1(10, 10);
cout << p1;
cout << p1 << endl;
cout << p1 << "hello world" << endl; } int main() { test02(); return 0; }
|
总结:重载左移运算符配合友元可以实现输出自定义数据类型
4.5.3 递增运算符重载
作用: 通过重载递增运算符,实现自己的整型数据
class MyInteger {
friend ostream& operator<<(ostream& out, MyInteger myint);
public: MyInteger() { m_Num = 0; } MyInteger& operator++() { m_Num++; return *this; }
MyInteger operator++(int) { MyInteger temp = *this; m_Num++; return temp; }
private: int m_Num; };
ostream& operator<<(ostream& out, 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();
system("pause");
return 0; }
|
自己写的:
#include<iostream> using namespace std;
class MyInteger { friend ostream& operator<<(ostream& cout, MyInteger myint); public: MyInteger() { m_Num = 0; } MyInteger& operator++() { m_Num++; return *this; }
MyInteger operator++(int) { MyInteger temp = *this; m_Num++; return temp; }
private: int m_Num; };
ostream& operator<<(ostream& cout, MyInteger myint) { cout << myint.m_Num; return cout; }
void test01() { MyInteger myint; cout << myint << endl; }
void test02() { MyInteger myint1;
cout << ++myint1 << endl;
cout << ++(++myint1) << endl;
cout << myint1 << endl;
}
void test03() { MyInteger myint2; cout << myint2++ << endl;
cout << myint2 << endl; } int main() { test02(); test03(); return 0; }
|
总结: 前置递增返回引用,后置递增返回值
4.5.4 赋值运算符重载
c++编译器至少给一个类添加4个函数
- 默认构造函数(无参,函数体为空)
- 默认析构函数(无参,函数体为空)
- 默认拷贝构造函数,对属性进行值拷贝
- 赋值运算符 operator=, 对属性进行值拷贝
如果类中有属性指向堆区,做赋值操作时也会出现深浅拷贝问题
示例:
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(18);
Person p2(20);
Person p3(30);
p3 = p2 = p1;
cout << "p1的年龄为:" << *p1.m_Age << endl;
cout << "p2的年龄为:" << *p2.m_Age << endl;
cout << "p3的年龄为:" << *p3.m_Age << endl; }
int main() {
test01();
system("pause");
return 0; }
|
自己写的:
#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; }
m_age = new int(*p.m_age);
return *this; } int *m_age; }; void test01() { Person p1(18); cout << p1.m_age << endl;
cout << *p1.m_age << endl;
Person p2(20);
cout << *p2.m_age << endl;
p2 = p1;
cout << *p2.m_age << endl;
Person p3(30); p3 = p2 = p1;
cout << *p3.m_age << endl; } int main() { test01();
int a = 10; int b = 20; int c = 30;
c = b = a;
return 0; }
|
4.5.5 关系运算符重载
作用:重载关系运算符,可以让两个自定义类型对象进行对比操作
示例:
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("孙悟空", 18); Person b("孙悟空", 18);
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; }
|
自己写的:
#include<iostream> 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 1; } return 0; }
bool operator!=(Person& p) { if (this->m_age != p.m_age || this->m_name != p.m_name) { return 1; } return 0; }
string m_name; int m_age; }; void test01() { Person p1("Sam", 18); Person p2("Sam", 18); if (p1 == p2) { cout << "p1==p2" << endl; } else { cout << "p1!=p2" << endl; }
if (p1 != p2) { cout << "p1!=p2" << endl; } else { cout << "p1==p2" << endl; } } int main() { test01(); return 0; }
|
4.5.6 函数调用运算符重载
- 函数调用运算符 () 也可以重载
- 由于重载后使用的方式非常像函数的调用,因此称为仿函数
- 仿函数没有固定写法,非常灵活
示例:
class MyPrint { public: void operator()(string text) { cout << text << endl; }
}; void test01() { MyPrint myFunc; myFunc("hello world"); }
class MyAdd { public: int operator()(int v1, int v2) { return v1 + v2; } };
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; }
|
自己写的:
#include<iostream> using namespace std;
class MyPrint { public: void operator()(string test) { cout << test << endl; } };
void MyPrint2(string test) { cout << test << endl; }
void test01() { MyPrint myPrint; myPrint("Hello World");
MyPrint2("Hello World"); }
class MyAdd { public: int operator()(int num1, int num2) { return num1 + num2; } };
void test02() { MyAdd myadd; int ret = myadd(100, 100);
cout << "ret=" << ret << endl;
cout << MyAdd()(100, 100) << endl;
} int main() { test01(); test02();
return 0; }
|