主页 > 开源代码  > 

C++运算符重载的学习笔记

C++运算符重载的学习笔记
加号运算符重载 两种方式 成员函数重载加号 #include <iostream> using namespace std; //加号运算符重载 class Person { public: // 1.成员函数重载加号 Person operator+(Person&p) { Person tmp; tmp.m_A = this->m_A + p.m_A; tmp.m_B = this->m_B + p.m_B; return tmp; } public: int m_A; int m_B; }; void test01() { Person p1; p1.m_A = 10; p1.m_B = 10; Person p2; p2.m_A = 10; p2.m_B = 10; //成员函数的本质调用 Person p3 = p1.operator+(p2); //Person p3 = p1 + p2; cout << "p3.m_A = " << p3.m_A << endl; cout << "p3.m_B = " << p3.m_B << endl; } int main() { test01(); system("pause"); return 0; } 全局函数重载加号 #include <iostream> using namespace std; //加号运算符重载 class Person { public: int m_A; int m_B; }; //2.全局函数重载 Person operator+(Person&p1, Person& p2) { Person tmp; tmp.m_A = p1.m_A + p2.m_B; tmp.m_B = p1.m_B + p2.m_B; return tmp; } 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 p3 = p1 + p2; cout << "p3.m_A = " << p3.m_A << endl; cout << "p3.m_B = " << p3.m_B << endl; } int main() { test01(); system("pause"); return 0; }

3.在运算符重载中也可以发生函数重载

#include <iostream> using namespace std; //加号运算符重载 class Person { public: int m_A; int m_B; }; //函数重载的版本 Person operator+(Person& p1, int a) { Person tmp; tmp.m_A = p1.m_A + a; tmp.m_B = p1.m_B + a; return tmp; } void test01() { Person p1; p1.m_A = 10; p1.m_B = 10; Person p2; p2.m_A = 10; p2.m_B = 10; //Person p3 = p1 + p2; //运算符重载,也可以发生函数重载 Person p4 = p1 + 100; cout << "p4.m_A = " << p4.m_A << endl; cout << "p4.m_B = " << p4.m_B << endl; } int main() { test01(); system("pause"); return 0; } 由于编译器不知道我们的自定义类型是如何进行相加的,所以我们必须要创建一个全局的或者构造的函数来重载这个加号,以实现我们的目的
左移运算符重载 #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 << p.m_A << " " << p.m_B << endl; return cout; } void test01() { Person p(10, 10); // p.m_A = 10; // p.m_B = 10; // cout << p.m_A << " " <<p.m_B << endl; cout << p << " GUET" << endl; } int main() { test01(); return 0; } 因为要保持cout << 这样的格式,所以在左移运算符重载中我们只用全局函数来进行重载由于我们要保留链式的特性,那么重载函数的返回值必须是对ostream&这种类型,保证返回的是同一个“东西“,才能在原来的基础上继续输出由于类中的成员变量为私有,我们必须在类中声明一下重载函数为友元
赋值运算符重载 #include <iostream> using namespace std; class MyInt { friend ostream& operator<<(ostream& cout, MyInt myInt); public: MyInt() { m_Num = 0; } MyInt& operator--() { //先-- m_Num--; //再返回 return *this; } MyInt operator--(int) { MyInt tmp = *this; m_Num--; return tmp; } private: int m_Num; }; //写后置的时候会报错 ostream& operator<<(ostream& cout, MyInt myInt)//这里的MyInt我用了引用会怎么样? { cout << myInt.m_Num << endl; } void test01() { MyInt myint; cout << --(--myint) << endl; } void test02() { MyInt myint; cout << myint-- << endl; cout << myint << endl; } int main() { test02(); //test01(); return 0; } 在赋值运算符的重载中要注意前置和后置的区别。 MyInt& operator--() { //先-- m_Num--; //再返回 return *this; } MyInt operator--(int) { MyInt tmp = *this; m_Num--; return tmp; } 返回值类型不一样,一个是返回同一个东西,另一个则是返回–之前的值由于函数的重载不能仅靠返回值不同来区分,所以有一个函数的形参部分有个int类型来区分两个函数

原因: 因为我们进行前置–,要对类创建的对象的成员变量进行修改,所以我们直接修改它本身然后返回它本身。而后置,我们不能先return否则函数提前结束,所以只能用一个临时变量来存储它被修改之前的值,然后对它本身进行修改再返回那个临时值。 同时,由于临时变量在函数结束时会销毁,那么再用引用会导致非法访问


关系运算符重载 #include <iostream> using namespace std; class Person { public: Person(string name, int age) { m_Name = name; m_Age = age; } bool operator==(Person& p) { if (m_Name == p.m_Name && m_Age == p.m_Age) { return true; } return false; } bool operator!=(Person& p) { if (m_Name == p.m_Name && m_Age == p.m_Age) { return false; } return true; } string m_Name; int m_Age; //重载 == 号 }; void test01() { Person p1("Tom", 19); Person p2("Tom", 18); if (p1 == p2) { cout << "p1 和 p2 是相等的" << endl; } else { cout << "p1 和 p2 不相等" << endl; } } int main() { test01(); return 0; }
函数调用运算符重载 #include <iostream> using namespace std; //函数调用运算符重载 //打印输出类 class MyPrint { public: void operator() (string text) { cout << text << endl; } }; //相加类 class MyAdd { public: int operator() (int a, int b) { return a + b; } }; void test01() { MyPrint p; p("hello GUET"); } void test02() { MyAdd add; cout << add(100, 200) << endl; //匿名函数对象 cout << MyAdd()(200, 500) << endl; } int main() { //test01(); test02(); return 0; }
标签:

C++运算符重载的学习笔记由讯客互联开源代码栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“C++运算符重载的学习笔记