C++运算符重载的详细讲解

加号运算符重载

对于内置数据类型,编译器知道如何运算

但是对于自己封装的类,编译器无法进行运算

这时可以通过自己定义运算符重载进行运算

operator+

通过成员函数重载+号

#include<iostream>
using namespace std;
class Person
{
public:
 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;
 }
};
void test01()
{
 Person p1;
 p1.m_a = 66;
 p1.m_b = 44;
 Person p2;
 p2.m_a = 6;
 p2.m_b = 4;
 Person p3;
 //通过函数原型调用
 p3 = p1.operator+(p2);
 //简便调用
 //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;
}

注意两种调用方式

通过函数原型调用

p3 = p1.operator+(p2);

简便调用

p3 = p1 + 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 test01()
{
 Person p1;
 p1.m_a = 66;
 p1.m_b = 44;
 Person p2;
 p2.m_a = 6;
 p2.m_b = 4;
 Person p3;
 //函数原型调用
 p3 = operator+(p1,p2);
 //简便调用
 //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;
}

注意两种调用方式

通过函数原型调用

p3 = operator+(p1,p2);

简便调用

p3 = p1 + p2;

运算符重载发生函数重载

运算符重载可以发生函数重载:Person+int等等

#include<iostream>
using namespace std;
class Person
{
public:
 int m_a;
 int m_b;
};
//通过全局函数实现重载
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 = 66;
 p1.m_b = 44;
 Person p2;
 p2.m_a = 6;
 p2.m_b = 4;
 Person p3;
 //函数原型调用
 //p3 = operator+(p1,55);
 //简便调用
 p3 = p1 + 55;
 cout << "p3.m_a:" << p3.m_a << endl;
 cout << "p3.m_b:" << p3.m_b << endl;
}
int main()
{
 test01();
 system("pause");
 return 0;
}

调用方法和定义方法与上面相同,不再多余赘述

总结

1、系统内置数据类型的表达式不可改变

2、不要滥用运算符重载

左移运算符

不利用成员函数重载左移运算符

没有具体演示,因为报错,我也没写出来

下面通过全局函数实现

#include<iostream>
using namespace std;
class Person
{
public:
 int m_a;
 int m_b;

};
ostream& operator<<(ostream& cout,Person&p)
{
 cout << "p.m_a=" <<p. m_a << " p.m_b=" <<p. m_b << endl;
 return cout;
}
void test01()
{
 Person p1;
 p1.m_a = 44;
 p1.m_b = 66;
 cout << p1 << endl;
}
int main()
{
 test01();
 system("pause");
 return 0;
}

因为要实现链式,实现追加,所以返回值必须是ostream

总结

配合友元实现自定义输出类型

递增运算符重载

递增运算符重载

#include<iostream>
using namespace std;
class myInt
{
 friend ostream& operator<<(ostream& cout, myInt num);
public:
 myInt()
 {
 this->m_a = 0;
 }
 //前置++运算符重载
 myInt& operator++()//返回引用是为了一直对一个数据进行递增,否则函数默认返回一个新的数
 {
 //先进行++
 m_a++;
 //然后返回自身
 return *this;
 }
 //后置++运算符重载
 myInt operator++(int)//int表示占位参数,用于区分前置后置参数
 {
 //先记录当前的值
 myInt temp=*this;
 //再递增
 m_a++;
 //然后返回记录的值
 return temp;
 }
private:
 int m_a;
};
//左移运算符重载
ostream& operator<<(ostream& cout, myInt num)
{
 cout << num.m_a;
 return cout;
}
void test01()
{
 myInt myint;
 cout << myint << endl;
 cout << ++myint << endl;
 cout << myint << endl;

}
int main()
{
 test01();
 system("pause");
 return 0;
}

赋值运算符重载

#include<iostream>
using namespace std;
class Person
{
public:
 Person(int num)//将数据开辟到堆区
 {
 m_a = new int(num);
 }
 ~Person()
 {
 if (m_a != NULL)
 {
  delete m_a;
  m_a = NULL;
 }
 }
 //重载赋值运算符
 Person& operator=(Person &p)//返回值用Person返回本身,可执行连等
 {
 //先判断是否有属性在堆区,如果有先释放干净
 if (m_a != NULL)
 {
  delete m_a;
  m_a = NULL;
 }
 m_a = new int(*p.m_a);
 return *this;
 }
 int* m_a;

};
void test01()
{
 Person p1(18);
 Person p2(209);
 Person p3(9);
 p2 = p1 = p3;
 cout << *p1.m_a << endl;
 cout << *p2.m_a << endl;
 cout << *p3.m_a << endl;
}
int main()
{
 test01();
 system("pause");
 return 0;
}

关系运算符重载

#include<iostream>
using namespace std;
#include<string>
class Person
{
public:

 Person(string name, int age)
 {
 this->age = age;
 this->name = name;
 }
 bool operator==(Person& p)
 {
 if (this->age == p.age && this->name == p.name)
 {
  return true;
 }
 else {
  return false;
 }
 }
 bool operator!=(Person& p)
 {
 if (this->age == p.age && this->name == p.name)
 {
  return false;
 }
 else {
  return true;
 }
 }
 string name;
 int age;

};
void test01()
{
 Person p1("gouride", 19);
 Person p2("gouride", 19);
 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();
 system("pause");
 return 0;
}

函数调用重载

仿函数

#include<iostream>
using namespace std;
#include<string>
class Myprint
{
public:
 void operator()(string name)
 {
 cout << name << endl;
 }
 int operator()(int a,int b)
 {
 return a + b;
 }
};
void test01()
{

 Myprint myprint;
 myprint("测试");
 //匿名对象调用
 cout << Myprint()(4,6) << endl;
}
int main()
{
 test01();
 system("pause");
 return 0;
}

总结

到此这篇关于C++运算符重载的文章就介绍到这了,更多相关C++运算符重载内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • 深入解析C++编程中的运算符重载

    C++中预定义的运算符的操作对象只能是基本数据类型,实际上,对于很多用户自定义类型,也需要有类似的运算操作.例如: class complex { public: complex(double r=0.0,double I=0.0){real=r;imag=I;} void display(); private: double real; double imag; }; complex a(10,20),b(5,8); "a+b"运算如何实现?这时候我们需要自己编写程序来说明"

  • 解析C++中不能重载为友元函数的四个运算符

    C++规定有四个运算符 =, ->, [], ()不可以是全局域中的重载(即不能重载为友员函数),这是为什么呢?现在先说说赋值运算符"="的重载C++规定赋值运算符"="只能重载为类的非静态成员函数,而不可以重载为类的友元函数.不能重载为类的静态成员应该比较容易理解,因为静态成员函数是属于整个类的,不是属于某个对象的,它只能去操作类静态数据成员.而赋值运算符"="是基于对象操作的.那么为什么赋值运算符不可以重载为类的友元函数?像同样都是双目

  • 详解C++编程中一元运算符的重载

    可重载的一元运算符如下: !(逻辑"非") &(取址) ~(二进制反码) *(取消指针引用) +(一元加) -(一元求反) ++(递增) --(递减) 转换运算符 后缀递增和递减运算符(++ 和 ––)在递增和递减中单独处理,下面会讲到. 以下规则适用于所有其他一元运算符.若要将一元运算符函数声明为非静态成员,则必须用以下形式声明它: ret-type operator op () 其中 ret-type 是返回类型,op 是上表中列出的运算符之一. 若要将一元运算符函数声明为

  • C++中不能被重载的运算符介绍

    C/C++ 里大多数运算符都可以在 C++ 中被重载.C 的运算符中只有 . 和 ?:(以及 sizeof,技术上可以看作一个运算符)不可以被重载.C++ 增加了一些自己的运算符,除了 :: 和 .* 外,大多数都可以被重载.

  • 详解c/c++赋值函数(重载=号运算符)

    首先c++里的各种运算符都是用函数实现的,比如=,就等号函数. 所以当用=给一个对象赋值的时候,实际调用的是=号所对应的=号函数. 分析下面的代码 #include <iostream> using namespace std; class Test{ public: explicit Test(){ data = 0; } explicit Test(int d):data(d){ cout << "C:" << this << &qu

  • C++运算符重载规则详解

    C++允许重载的运算符和不允许重载的运算符 C++中绝大部分的运算符允许重载,具体规定见表 不能重载的运算符只有5个: .  (成员访问运算符) .*  (成员指针访问运算符) ::  (域运算符) sizeof  (长度运算符) ?:  (条件运算符) 前两个运算符不能重载是为了保证访问成员的功能不能被改变,域运算符和sizeof 运算符的运算对象是类型而不是变量或一般表达式,不具备重载的特征. C++运算符重载的规则 C++对运算符重载定义了如下几条规则. 1) C++不允许用户自己定义新的

  • C++重载运算符的规则详解

    (1)C++不允许用户自己定义新的运算符,只能对已有的C++运算符进行重载.例如,有人觉得BASIC中用"* *"作为幂运算符很方便,也想在C++中将"* *"定义为幂运算符,用"3* *5"表示35,这是不行的. (2)C++允许重载的运算符C++中绝大部分运算符都是可以被重载的. 不能重载的运算符只有5个: .             (成员访问运算符) .*            (成员指针访问运算符) ::             (域运

  • c++ *运算符重载

    运算符重载,对象和指向对象的指针 直接上code 复制代码 代码如下: #include <iostream> using namespace std;  class test {     public:         int a;         test() : a(0){}         test &operator*(){             cout << "operator*" << endl;             c

  • C++运算符重载的方法详细解析

    运算符重载实质上是函数的重载 重载运算符的函数一般格式如下: 函数类型    operator  运算符名称    (形参表列) {对运算符的重载处理} 例如,想将"+"用于Complex(复数)的加法运算,函数的原型可以是这样的: 复制代码 代码如下: Complex operator + (Complex & c1,Complex &c2); 其中,operator是关键字,时候专门用于定义重载运算符的函数的,运算符名称就是C++提供给用户的预定运算符. 注意:函数

  • c++运算符重载基础知识详解

    实际上,很多C++运算符已经被重载.eg:将*运算符用于地址,将得到存储在这个地址中的值,将他用于2个数字时,得到的将是他们的乘积.C++根据操作数的数目和类型来决定采用哪种操作. C++允许将运算符重载扩展到用户定义的类型.例如,允许使用+将两个对象相加.编译器将根据操作数的数目和类型决定使用加法定义.运算符重载可以使代码看起来更自然.例如,将2个数组相加是一种常见的运算.通常,需要使用下面这样的for循环来实现: 复制代码 代码如下: for (int i = 0; i < 20; i++)

随机推荐