IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> C++翁恺学习27-运算符重载-基本规则 -> 正文阅读

[C++知识库]C++翁恺学习27-运算符重载-基本规则

overloaded operators? 重载几乎所有的运算符?

  • allows user-defined types to act like built in types

  • another way to make a function call

  • unary and binary operators can be overloaded:

    + - * / % ^(异或) & | ~
    = < > += -= *= /= %=
    ^= &=  |= << >> <<= >>= ==
    != <= >= ! && || ++ --
        , ->* -> () []

    operator new operator delete

    operator new[] operator delete[]

operators you can't overload 不能重载

	. .* :: ?:
	sizeof typeid
    static_cast dynamic_cast const_cast
    reinterpret_cast

Restrictions? ?限制条件

  • only existing operators can be overloaded(you can't create a ** operator for exponentiation)? 已经存在的运算符可以被重载
  • operators must be overloaded on a class or enumeration type??
  • overloaded operators must:
    • preserve number of operands? ?保持原有操作数的个数
    • preserve precedence(优先级)? 保持优先级

c++ overloaded operator

  • just a function with an operator name!? 写一个函数

    • use the "operator" keyword as a prefix to name

      operator *(...)  // 重载 * 号
  • can be a member function? 成员函数

    • implicit first argument
    const String String::operator +(const String& that);  //this + that
  • can be a global (free) function? 全局函数

    • both arguments explicit
    const String operator +(const String& r,const String& l);

how to overload

  • an member function? ? 成员函数
    • implicit first argument? ? this是第一个参数
    • no type conversion performed on receiver(运算符左值)? ??
    • must have access to class definition

在编译的时候,能看到operator+的函数体默认变成内联函数,而且返回值只是为了能返回,在函数里并没有用,所以在这个函数里的拷贝构造是被优化的。

使用成员函数,编译器从左到右扫描,看用谁的运算符。

1. x + y, ? ? ? ?编译器从左到右扫描,用x的operator+,两个intrger类型;

2. z = x + 3, ?编译器从左到右扫描,用x的operator+,integer里面有函数将int 3转化为integer的类型(构造函数);

3. z = 3 + y, ? ?编译器从左到右扫描,用int 3的+,integer里面没有将nteger的类型转化为int类型的函数;×

4. z = x + 3.5 ,用x的operator+,double 到 int 不能自动转换,需要强制类型转换??×?

  • for binary operaters(+,-,*,etc) member functions require one argument.? 二元运算符需要一个参数

  • for unary operators (unary -,!,etc) member functions require no arguments:? ?一元不改自己,制造出来一个新的对象来

    const Integer operator-() const{
        return Integer(-i);
    }
    ...
    z=-x;    //z.operator=(x.oprator-()); 

?operator- ,this仍然是const,不改自己,产生一个新的对象。

  • as a global function? ?两个算子都要写上
    • explicit first argument
    • type conversions performed on both arguments
    • can be made a friend
    • type conversions performed on both arguments

global operators(friend)

class Integer{
    friend const Integer operator+(const Integer&rhs,const Integer& lhs); // 
    ...
}
const Integer operator+(const Integer&rhs,const Integer& lhs){  // 全局函数
    return Integer(lhs.i,rhs.i);
}
  • binary operators require two arguments

  • unary operators require one argument

  • conversion

    • z=x+y;//ok

    • z=x+3;//ok

    • z=3+y;//ok

    • z=3+7;//ok

 
  • if you don't have access to private data members, then the global function must use the pubic interface

tips: members vs. free functions

  • unary operators should be members 单目的应该是成员的

  • = () -> ->* must be members 必须

  • assignment operators should be members

  • all other binary operators as non-members 非成员的

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2022-04-26 11:24:29  更:2022-04-26 11:28:19 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/21 0:06:29-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码