C++运算符重载

来源:互联网 发布:mac电脑如何制作铃声 编辑:程序博客网 时间:2024/04/23 19:49

一、概念

1、什么是运算符重载

所谓重载,就是重新赋予新的含义。

2、运算符重载的限制



二、运算符重载的基础

1、运算符重载本质是一个函数,函数名字组成:operator+要重载的运算符

2、运算符重载的步骤:

(1)写函数名:operator+要重载的函数名

(2)根据运算需求写函数的参数列表

(3)根据需求写出函数返回值的类型

#include <stdio.h>class Complex{public:Complex(){m_a = 0;m_b = 0;}Complex(int a,int b){m_a = a;m_b = b;}void print(){printf("%d + %di\n",m_a,m_b);}Complex operator+(Complex &b){Complex tmp(m_a + b.m_a,m_b + b.m_b);return tmp;}Complex operator-(Complex &b){Complex tmp(m_a - b.m_a,m_b - b.m_b);return tmp;}private:int m_a;int m_b;};int main(){Complex c1(1,2),c2(2,3),c4(2,2),c3;c1.print();c2.print();c3 = c1 + c2 - c4;c3.print();return 0;}


三、前置和后置操作符重载

1、C++中通过一个占位参数来区分前置运算和后置运算

#include <stdio.h>class Complex{public:Complex(){m_a = 0;m_b = 0;}Complex(int a,int b){m_a = a;m_b = b;}void print(){printf("%d + %di\n",m_a,m_b);}Complex operator+(Complex &b){Complex tmp(m_a + b.m_a,m_b + b.m_b);return tmp;}Complex &operator++(){++m_a;++m_b;return *this;}Complex operator++(int){Complex tmp(m_a,m_b);m_a++;m_b++;return tmp;}private:int m_a;int m_b;};int main(){Complex c1(1,2),c2(2,3),c4(2,2),c3;c1.print();c2.print();c3 = (c1++) + (++c1) + (++c1);c3.print();c1.print();return 0;}


四、友元函数实现操作符重载

1、当无法修改左操作数的类时,使用全局函数进行重载

2、友员函数重载运算符常用于运算符的左右操作数类型不同的情况

#include <iostream>using namespace std;class Complex{friend ostream &operator<<(ostream &out,const Complex &obj);public:Complex(){m_a = 0;m_b = 0;}Complex(int a,int b){m_a = a;m_b = b;}private:int m_a;int m_b;};ostream &operator<<(ostream &out,const Complex &obj){out << obj.m_a << "+" << obj.m_b << "i";return  out;}int main(){Complex c1(1,2);cout << c1 << endl;return 0;}


五、赋值运算符重载

1、赋值运算符重载函数必须为成员函数

2、如果没写自定义的 赋值运算符重载,编译器会有一个默认的赋值运算符重载函数,默认赋值运算符做的是一些简单的拷贝,是浅拷贝

#include <iostream>#include <string.h>using namespace std;class Student{friend ostream &operator<<(ostream &out,const Student &obj);public:Student(){m_id = 0;m_name = NULL;}Student(int id,char *name){m_id = id;int len = strlen(name);m_name = new char[len + 1];strcpy(m_name,name);}Student(const Student &obj){m_id = obj.m_id;int len = strlen(obj.m_name);m_name = new char[len + 1];strcpy(m_name,obj.m_name);}Student &operator=(const Student &obj){if(this == &obj)return *this;if(m_name != NULL){delete []m_name;m_name = NULL;}int len = strlen(obj.m_name);m_name = new char[len + 1];m_id = obj.m_id;strcpy(m_name,obj.m_name);return *this;}~Student(){if(m_name != NULL){delete []m_name;m_name = NULL;}m_id = 0;}private:int m_id;char *m_name;};ostream &operator<<(ostream &out,const Student &obj){out << "id = " << obj.m_id << ",name = " << obj.m_name;return out;}int main(){Student s1(10,"wang");Student s2 = s1;Student s3;s3 = s3 = s1;cout << s1 << endl;cout << s2 << endl;cout << s3 << endl;return 0;}


六、括号运算符重载

#include <stdio.h>class Test{public:Test(int a){m_a = a;}void print(){printf("a = %d\n",m_a);}void operator()(int num){printf("a = %d\n",num);}private:int m_a;};int main(){Test t(10);t.print();t(20);return 0;}


七、逻辑与和逻辑或

1、&&和||是C++中非常特殊的操作符 

2、&&和||内置实现了短路规则 

3、操作符重载是靠函数重载来完成的 

4、操作数作为函数参数传递 

5、C++的函数参数都会被求值,无法实现短路规则