C++运算符重载(超全)

C++运算符重载

1)

运算符重载的本质为函数重载,但有一定的规则需要遵循。[1]
(1)重载运算符时,运算符的运算顺序和优先级不变,操作数个数不变。

(2)不能创造新的运算符,只能重载C++中已有的运算符,并且规定有6个运算符不能重载,如表所示。

不能重载的运算符含义
.类属关系运算符
.*成员指针运算符
::作用域运算符
?:条件运算符
#编译预处理符号
sizeof取数据类型的长度

(3)运算符重载是针对新类型的实际需求,对原有的运算符进行适当的改造。一般来讲,重载后的运算符的功能应当与运算符的实际意义相符。

2)

由于运算符种类与数量较多,我在此将运算符重载粗浅分为4类:1.赋值运算符的重载,2.算术运算符的重载,3.关系运算符的重载,4.其他较为特殊的重载。

1.赋值运算符的重载

赋值运算符即为“=”;赋值运算符的重载即为对“=”的重载。
模板为 classname &operator=(classname& 实例对象)
以class HugeInteger为例

#include <cstring>
#include <sstream>
class HugeInteger{
public:
	HugeInteger& operator=(const HugeInteger &h);
private:
    string str;
};

HugeInteger& HugeInteger::operator=(const HugeInteger &h)
{
    this->str=h.str;
    return *this;
}

2.算术运算符的重载

算术运算符包括+、++、–、- -、+=、-=、/、*等。在此只以+、-、+=为例。
模板为 classname &operator运算符(classname& 实例对象)
以class complex为例

#include<iostream>
using namespace std;
class complex{
public:
    complex(double r=0,double i=0){
        this->r=r;
        this->i=i;
    }
    complex operator+(complex&);
    complex operator-(complex&);
    complex operator+=(complex&);
private:
    double r,i;
};
complex complex::operator+(complex &c){
    return complex(r+c.r,i+c.i);
}
complex complex::operator-(complex &c){
    return complex(r-c.r,i-c.i);
}
complex complex::operator+=(complex &c){
    r+=c.r;
    i+=c.i;
    return *this;//return complex(r,i)亦可
}

3.关系运算符的重载

关系运算符包括> ,< ,>= ,==,!=等;在此只以>,==为例。
模板为 friend bool &operator运算符(const classname& 实例对象1,const classname& 实例对象2)
以class String为例

#include <iostream>
#include <string> 
#include <cstring> 
using namespace std;
class String
{
private:
    string str;   
public:
	String(string s){str=s;}
	friend bool operator>(const String &st1, const String &st2);
	friend bool operator==(const String &st, const String &st2);
};

bool operator>(const String &st1, const String &st2)
{
    return (std::strcmp(st1.str, st2.str) > 0);
}
bool operator==(const String &st1, const String &st2)
{
    return (std::strcmp(st1.str, st2.str) == 0);
}

4.其他

在这里给出重载<<,>>,[]的方法
模板:
(1)cout<<:friend ostream &operator<<(ostream &cout,const classname& 实例对象) / ostream &operator<<(ostream &cout)
(2)cin>>:friend istream &operator>>(istream &cin,classname& 实例对象) / istream &operator>>(istream &cin)
(3)[ ]:typename & operator[ ] (形参表)

仍旧以class String为例

#include <iostream>
#include <string> 
#include <cstring> 
using namespace std;
class String
{
private:
    string str;   
public:
	String(string s){str=s;}
	char & operator[](int i);
	friend ostream & operator<<(ostream & cout, const String & st);
    friend istream & operator>>(istream & cin, String & st);
};

char &String::operator[](int i)
{
    return str[i];
}
ostream &operator<<(ostream & cout, const String &st)
{
    cout << st.str;
    return cout;
}

istream &operator>>(istream & cin, String &st)
{
    char temp[40];
    is.get(temp, 40);
    if (cin)
        st = temp;
    while (cin && cin.get() != '\n')
        continue;
    return cin;
}

[1]刘瑞芳,肖波,许桂平,孙勇,徐惠民 . C++语言程序设计[M] . 西安:西安电子科技大学出版社 , 2017 .

  • 81
    点赞
  • 626
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值