zoukankan      html  css  js  c++  java
  • 运算符重载

    重载运算符主要是为了习惯使用方式,其最明显应用部分是针对简单的用户定义类型。只有充分理由时才重载操作符,而且应该保持其自然语义。

    • 为什么要使用重载运算符

    C++本身提供了大量的内部数据类型,如果没有涉及到用户的自定义类型,比如枚举(enum)、结构体(struct)对字符串的操作和类(class)时。人们很少用到运算符重载。但在对用户自定义类型的成员数据使用的时候。比如:复数相加、类中输入输出运算符的重载的重载。其实,运算符重载是一个语法糖。通常来说使用语法糖能够增加程序的可读性,从而减少程序代码出错的机会。但不提倡用

    • 运算符重载声明格式

    使用成员函数重载运算符:返回类型  函数名(参数表) ;     如  unsigned int operator()() ;

    使用友元函数重载运算符:在成员函数重载前面加上关键字 friend 即可。

    • 运算符重载实现的方式:使用成员函数重载和使用友元函数重载。
    1. 成员函数重载运算符

    请大家看一段代码:

     1 #include<iostream>
     2 using namespace std;
     3 
     4 class Increase
     5 {
     6 private:
     7     unsigned value;
     8 public:
     9     Increase() {value=0;}
    10     void display() const { cout <<value<<'\n';}
    11     Increase operator ++ ();//前置
    12     Increase operator ++ (int);//后置
    13 };
    14 
    15 Increase Increase::operator++()
    16 {
    17     value++;
    18     return *this;
    19 }
    20 
    21 Increase Increase::operator++(int)
    22 {
    23     Increase temp;
    24     temp.value = value++;
    25     return temp;
    26 }
    27 
    28 void main()
    29 {
    30     Increase a,b,n;
    31     for (int i=0;i<10;i++)
    32         a=n++;
    33     cout << "n=";
    34     n.display();
    35     cout << "a=";
    36     a.display();
    37     for (int j=0;j<10;j++)
    38         b=++n;
    39     cout << "n=";
    40     n.display();
    41     cout << "b=";
    42     b.display();
    43     system("pause");
    44 }

    运行结果:

    分析:以上代码是重载单目运算符++,其中有++的前置和后置。

    前置:A::A operator++() 我们可以理解为 object.operator++()  对象的this *指针触发激活成员函数operator++()

    后置:A::Aoperator++(int)我们可以理解为object.operator++(0)   为了编译器区分前置和后置++,c++规定后缀形式有一个int类型参数

    2.友元函数重载运算符

    这位朋友很好的应用了友元函数重载输入输出运算符。以下摘录他一段代码示之

      9 class student
     10 {
     11 public:
     12     bool del;//删除标记
     13     string name;//姓名
     14     string num;//编号
     15     int age;//年龄
     16     int gender;//性别
     17     int grade;//成绩
     18     friend istream &operator>>(istream &A,student &B);
     19     friend ostream &operator<<(ostream &A,student &B);
     20 };
     21 
     22 
     23 //重载输入流
     24 istream &operator>>(istream &A,student &B)
     25 {
     26     cout<<"请输入姓名:";A>>B.name;
     27     cout<<"请输入年龄:";A>>B.age;
     28     cout<<"请输入性别:";A>>B.gender;
     29     cout<<"请输入学号:";A>>B.num;
     30     cout<<"请输入成绩:";A>>B.grade;
     31     return A;
     32 
     33 }
     34 
     35 
     36 //重载输出流
     37 ostream &operator<<(ostream &A,student &B)
     38 {
     39     A<<"姓名:"<<B.name<<endl;
     40     A<<"年龄:"<<B.age<<endl;
     41     A<<"性别:"<<((B.gender==0)?"":"")<<endl;
     42     A<<"学号:"<<B.num<<endl;
     43     A<<"成绩:"<<B.grade<<endl;
     44     return A;
     45 
     46 }

    分析:由于友元函数中,运算符的的操作对象都在参数表中,由于要改变对象的状态,其参数应该使用应用参数(&)

    3.成员函数和友元函数重载的区别

    通过以上代码的展示。对于重载大家都有一定的了解了(高手请忽略这句话)其中下表[]、调用()、间接->、 赋值=  如果要重载,必须重载为成员函数而不能重载为友元函数。

    网友对此解释为:

    为什么呢? 因为
    1. C++不允许全局重载这几个操作符.
    2. C++也没有提供调用对象作为参数放进去.

  • 相关阅读:
    Docker容器监控
    Docker Compose集成式应用组合及service编排
    Docker数据挂载
    Docker 构建私有仓库
    Dockerfile构建私有镜像
    Docker常用命令
    【手记】Reflexil直接让方法返回true或false
    【组件分享】自定义窗口标题菜单
    DLL/OCX文件的注册与数据执行保护DEP
    【SQL】用SSMS连接Oracle手记
  • 原文地址:https://www.cnblogs.com/hww836967373/p/3045477.html
Copyright © 2011-2022 走看看