运算符重载概念:对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型

1.加号运算符重载

作用:实现两个自定义数据类型相加的运算

#include <iostream>
#include <string>
using namespace std; //加号运算符重载
class Person{
public:
//1.成员函数重载+号
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;
}; //2.全局函数重载+号
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;
} //3.函数重载的版本
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 = p1.operator+(p2); //全局函数重载的本质调用
//Person p3 = operator+(p1,p2);
Person p3 = p1 + p2; //运算符重载也可以发生函数重载
Person p4 = p3 + 100; cout << "p3.m_a = " << p3.m_a <<endl;
cout << "p3.m_b = " << p3.m_b <<endl;
cout << "p4.m_a = " << p4.m_a <<endl;
cout << "p4.m_b = " << p4.m_b <<endl;
} int main(){
test01();
return 0;
}

总结1:对于内置的数据类型的表达式的运算符是不可能改变的

总结2:不要滥用运算符重载

2.左移运算符重载

作用:可以自输出自定义数据类型

#include <iostream>
#include <string>
using namespace std; //左移运算符重载
class Person{
public:
//1.利用成员函数重载左移运算符 p.operator<<(cout) 简化版本p<<cout
//通常不会利用成员函数重载左移运算符,因为无法实现cout在左侧
int m_a;
int m_b;
}; //2.利用全局函数重载左移运算符
ostream & operator<<(ostream &cout,Person &p){
cout << "m_a = " << p.m_a << " m_b = " << p.m_b << endl;
return cout;
//必须返回引用,因为只能存在一个,不能开辟新的空间
} void test01(){
Person p;
p.m_a = 10;
p.m_b = 10; cout << "p.m_a = " << p.m_a <<endl;
cout << "p.m_b = " << p.m_b <<endl;
cout << p << endl;
} int main(){
test01();
return 0;
}
  • 通常不会利用成员函数重载左移运算符,因为无法实现cout在左侧

    利用成员函数重载左移运算符 p.operator<<(cout) 简化版本p<<cout

  • 左移运算符重载必须返回ostream引用,因为只能存在一个,不能开辟新的空间

3. 递增运算符重载

作用:通过重载递增运算符,实现自己的整型数据

注意:前置返回的是变量的引用,后置返回的是常量。所以++++c合法,而c++++不合法

#include <iostream>
#include <string>
using namespace std; //重载递增运算符 //自定义整型
class MyInterger{
friend ostream & operator<<(ostream &cout, MyInterger myint);
public:
MyInterger(){
m_num = 0;
}
//1.重载前置++运算符
//返回引用是为了一直对一个数据进行递增操作
MyInterger & operator++(){
m_num = m_num+1;
return *this; //返回自身
}
//2.重载后置++运算符
//int代表一个占位参数,可以用于区分前置和后置递增
//后置递增一定不能返回引用,因为temp为局部变量,函数运行完后自动释放
MyInterger operator++(int){
//先 记录当时结果
MyInterger temp = *this;
//后 递增
m_num = m_num+1;
//最后 返回所记录结果
return temp;
}
private:
int m_num;
}; //重载左移运算符
ostream & operator<<(ostream &cout, MyInterger myint){
cout << myint.m_num;
return cout;
} void test01(){
MyInterger myint;
cout << ++myint << endl;
} void test02(){
MyInterger myint;
cout << myint++ << endl;
cout << myint << endl;
} int main(){
test02();
return 0;
}

总结:前置递增返回的是引用,后置递增返回的是值

4.赋值运算符重载

c++编译器至少给一个类添加4个函数

  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) {
//编译器提供的浅拷贝
//m_age = p.m_age
//应该先判断是否有属性在堆区,如果有先释放干净,然后再深拷贝
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);
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;
}

5.关系运算符重载

作用:重载关系运算符,可以让两个自定义类型对象进行对比操作

#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 test01() {
Person p1("xia",18);
Person p2("xia", 18); if (p1 == p2) {
cout << "p1 == p2" << endl;
}
else {
cout << "p1 != p2" << endl;
}
} int main() {
test01();
return 0;
}

6.函数调用运算符重载

  • 函数调用运算符()也可以重载
  • 由于重载后使用的方式非常像函数的调用,因此称为仿函数
  • 仿函数没有固定写法,非常灵活
#include<iostream>
#include<string>
using namespace std; //函数调用运算符的重载 //打印输出类
class MyPrint {
public:
//重载函数调用运算符
void operator()(string test) {
cout << test << endl;
}
}; class MyAdd {
public:
int operator()(int num1, int num2) {
return num1 + num2;
}
}; void MyPrint02(string test) {
cout << test << endl;
} void test01() {
MyPrint myPrint;
myPrint("hello world"); //由于使用起来非常类似于函数调用,因此称为仿函数
MyPrint02("hello world");
} void test02() {
MyAdd myadd;
int ret = myadd(100, 100);
cout << "ret = " << ret << endl;
//匿名函数对象
cout << MyAdd()(100, 100) << endl;
} int main() {
test01();
test02();
return 0;
}

C++学习(12)—— 运算符重载的更多相关文章

  1. C++学习之运算符重载的总结

    C++学习之运算符重载的总结              运算符重载是对已有的运算符赋予多重含义,使同一个运算符作用域不同类型的数据导致不同行为的发生,C++为运算符重载提供了一种方法,即运算符重载函数 ...

  2. C++学习笔记-运算符重载

    运算符重载使得用户自定义的数据以一种更简洁的方式工作 运算符重载规则 重载运算符的限制 可以重载的运算符 + - * / % ^ & | ~ ! = < > += -= *= /= ...

  3. 我的c++学习(8)运算符重载和友元

    运算符的重载,实际是一种特殊的函数重载,必须定义一个函数,并告诉C++编译器,当遇到该运算符时就调用此函数来行使运算符功能.这个函数叫做运算符重载函数(常为类的成员函数). 方法与解释 ◆ 1.定义运 ...

  4. C++学习26 运算符重载的概念和语法

    所谓重载,就是赋予新的含义.函数重载(Function Overloading)可以让一个函数名有多种功能,在不同情况下进行不同的操作.运算符重载(Operator Overloading)也是一个道 ...

  5. STL学习之运算符(<<)重载问题和仿函数的实现

    /*   运算符<<的重载一直报错,   友原函数中可以访问到类的私有成员*/#include<iostream>using namespace std; class MyIn ...

  6. 运算符重载+日期类Date

    Hello,一只爱学习的鱼 大学学习C++运算符重载的时候,老师出了一道"运算符重载+类"的综合练习题,让我们来一起看看吧! 题目: 设计一个日期类Date,包括年.月.日等私有成 ...

  7. 初步C++运算符重载学习笔记&lt;3&gt; 增量递减运算符重载

    初步C++运算符重载学习笔记<1> 初探C++运算符重载学习笔记<2> 重载为友元函数     增量.减量运算符++(--)分别有两种形式:前自增++i(自减--i).后自增i ...

  8. C++运算符重载学习总结

    在C ++中,我们可以使运算符适用于用户定义的类. 这意味着C ++能够为运算符提供数据类型的特殊含义,这种能力称为运算符重载. 例如,我们可以在像String这样的类中重载运算符'+',这样我们就可 ...

  9. C#高级编程笔记2016年10月12日 运算符重载

    1.运算符重载:运算符重重载的关键是在对象上不能总是只调用方法或属性,有时还需要做一些其他工作,例如,对数值进行相加.相乘或逻辑操作等.例如,语句if(a==b).对于类,这个语句在默认状态下会比较引 ...

随机推荐

  1. 创建java类中类出现is not an enclosing class

    public class A { public class B { } }; 需要实例B类时,按照正逻辑是,A.B ab = new A.B(); 那么编译器就会出现一个错误--"is no ...

  2. HSA AMD异构计算架构

    当前的CPU和GPU是分立设计的处理器,不能高效率地协同工作,编写同时运行于CPU和GPU的程序也是相当麻烦.由于CPU和GPU拥有独立的地址空间,应用程序不得不明确地控制数据在CPU和GPU之间的流 ...

  3. Failed to contact the endpoint at http://controller:35357/ for discovery. Fallback to using that endpoint as the base url.

    问题描述 openstack安装过程中,执行 openstack domain create --description "Domain" example 报错如下: Failed ...

  4. 注意:MagickReadImageBlob() 引发的问题

    今天发现: 如果之前的 mw 已加载了具体的图片数据后,再对这个 mw 进行: MagickReadImageBlob(mw, data, dataLen) 程序运行发生了崩溃. 最后找到原因: Ma ...

  5. magic模块 :Exception Value:failed to find libmagic. Check your installation

    原因 缺少安装依赖: magic 安装依赖: https://github.com/ahupp/python-magic#dependencies windows下解决方法: https://gith ...

  6. Kafka支持单集群20万分区

    Kafka支持单集群20万分区 之前网上关于确定Kafka分区数的博客多多少少都源自于饶军大神的文章,如今他带来了这方面的第二篇文章,特此翻译一下,记录一下其中的要点. 原贴地址: https://w ...

  7. 认识一下microbit扩展板robotbit

    在我们买到的套件中,另一块叫robotbit扩展板,所谓扩展板,是把控制板上的针脚引出来,方便我们扩展驱动连接电机.舵机.及各种传感器以展功能. 介绍如下 :下面 反面: 官方文档:http://le ...

  8. C# Convert.ChangeType()

    Convert.ChangeType() 将未知类型转换为已知类型 ; object result = Convert.ChangeType(content, typeof(int)); 其他常用的转 ...

  9. Big Data Hubris:"大数据傲慢"问题

    大数据的傲慢与偏见— 读后心得 数据模型研究者必看的书 原文链接: https://medium.com/@iven00000000/%E5%A4%A7%E6%95%B8%E6%93%9A%E7%9A ...

  10. 如何快速找到Chrome配置文件路径,MAC 与window 都适用

    Chrome 的配置文件主要用于存储浏览器的相关配置.书签.扩展插件和密码等,Chrome 配置文件会存储在用户计算机的一个单独文件夹当中,当你升级或重装浏览器时,这些已有配置将可以被完整保存下来. ...