栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++ 运算符重载

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++ 运算符重载

C++ 运算符重载
  • 1. 语法格式
    • 1.1 友元重载
    • 1.2 成员重载
    • 1.3 注意事项
  • 2. 重载规则
  • 3. 双目运算符重载
    • 3.1 成员函数重载+=实现
    • 3.2 友元函数重载+=实现
  • 4. 单目运算符重载
    • 4.1 友元函数重载++(前++)实现
    • 4.2 友元函数重载++(后++)实现
  • 4. 友元还是成员

运算符重载的本质是函数重载。

1. 语法格式
返值类型 operator 运算符名称(形参表列)
{
	重载实体;
}

operator 运算符名称 在一起构成了新的函数名。比如
const Complex operator+(const Complex &c1,const Complex &c2);
我们会说,operator+ 重载了重载了运算符+。

1.1 友元重载
#include 
using namespace std;
class Complex
{
public:
    Complex(float x=0, float y=0):_x(x),_y(y){}
    void dis()
    {
        cout<<"("<<_x<<","<<_y<<")"< 

1.2 成员重载
#include 
using namespace std;
class Complex
{
public:
    Complex(float x=0, float y=0):_x(x),_y(y){}
    void dis()
    {
        cout<<"("<<_x<<","<<_y<<")"<_x + another._x,this->_y + another._y);
}
int main()
{
    Complex c1(2,3);
    Complex c2(3,4);
    c1.dis();
    c2.dis();
    // Complex c3 = c1+c2;
    // Complex c3 = operator+(c1,c2);
    Complex c3 = c1+c2; //优先调用成员函数重载??
    c3.dis();
    return 0;
}

1.3 注意事项

关于运算符重载函数的返回值:
int a = 3;
int b = 4;
(a+b) = 100; 这种语法是错的,所以重载函数+的返回值加 const 是来修饰。
string a = “china”,b = “ is china”, c;
(c = a) = b; 此时的语法,是重载= 返回值不需加 const 。

2. 重载规则
  1. C++不允许用户自己定义新的运算符,只能对已有的 C++运算符进行重载。
  2. C++允许重载的运算符
  3. 不能重载的运算符
  4. 重载不能改变运算符运算对象(即操作数)的个数。
  5. 重载不能改变运算符的优先级别。
  6. 重载不能改变运算符的结合性。如,复制运算符”=“是右结合性(自右至左),重载后仍为右结合性。
  7. 重载运算符的函数不能有默认的参数,否则就改变了运算符参数的个数,与前面矛盾。
  8. 重载运算符的运算中至少有一个操作数是自定义类。重载的运算符必须和用户定义的自定义类型的对象一起使用,其参数至少应有一个是类对象(或类对象的引用)。也就是说,参数不能全部是 C++的标准类型,以防止用户修改用于标准类型数据成员的运算符的性质。
3. 双目运算符重载 3.1 成员函数重载+=实现
using namespace std;
class Complex
{
public:
    Complex(float x=0, float y=0):_x(x),_y(y){}
    void dis()
    {
        cout<<"("<<_x<<","<<_y<<")"<_x += c._x;
        this->_y += c._y;
        return * this;
    }
private:
    float _x;
    float _y;
};
int main()
{

    int a = 10, b = 20,c = 30;
    (a += b) += c;
    cout<<"a = "< 

4.2 友元函数重载++(后++)实现
#include 
using namespace std;
class Complex
{
public:
    Complex(float x=0, float y=0):_x(x),_y(y){}
    void dis()
    {
        cout<<"("<<_x<<","<<_y<<")"< 

4. 友元还是成员

1,一个操作符的左右操作数不一定是相同类型的对象,这就涉及到将该操作符函数定义为谁的友元,谁的成员问题。
2,一个操作符函数,被声明为哪个类的成员,取决于该函数的调用对象(通常是左操作数)。
3,一个操作符函数,被声明为哪个类的友员,取决于该函数的参数对象(通常是右操作数)。

假设,我们有类 Sender 类和 Mail 类,实现发送邮件的功能

Sender sender; Mail mail;
sender<< mail

sender 左操作数,决定了 operator<<为 Sender 的成员函数,而 mail 决定了operator<<要作 Mail 类的友员。

#include 
using namespace std;
class Mail;
class Sender
{
public:
    Sender(string s):_addr(s){}
    Sender& operator<<(const Mail & mail); //成员
private:
    string _addr;
};
class Mail
{
public:
    Mail(string _t,string _c ):_title(_t),_content(_c){}
    friend Sender& Sender::operator<<(const Mail & mail);
//友元
private:
    string _title;
    string _content;
};
Sender& Sender::operator<<(const Mail & mail)
{
    cout<<"Address:"<<_addr< 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/430114.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号