Mr Dk.'s BlogMr Dk.'s Blog
  • 🦆 About Me
  • ⛏️ Technology Stack
  • 🔗 Links
  • 🗒️ About Blog
  • Algorithm
  • C++
  • Compiler
  • Cryptography
  • DevOps
  • Docker
  • Git
  • Java
  • Linux
  • MS Office
  • MySQL
  • Network
  • Operating System
  • Performance
  • PostgreSQL
  • Productivity
  • Solidity
  • Vue.js
  • Web
  • Wireless
  • 🐧 How Linux Works (notes)
  • 🐧 Linux Kernel Comments (notes)
  • 🐧 Linux Kernel Development (notes)
  • 🐤 μc/OS-II Source Code (notes)
  • ☕ Understanding the JVM (notes)
  • ⛸️ Redis Implementation (notes)
  • 🗜️ Understanding Nginx (notes)
  • ⚙️ Netty in Action (notes)
  • ☁️ Spring Microservices (notes)
  • ⚒️ The Annotated STL Sources (notes)
  • ☕ Java Development Kit 8
GitHub
  • 🦆 About Me
  • ⛏️ Technology Stack
  • 🔗 Links
  • 🗒️ About Blog
  • Algorithm
  • C++
  • Compiler
  • Cryptography
  • DevOps
  • Docker
  • Git
  • Java
  • Linux
  • MS Office
  • MySQL
  • Network
  • Operating System
  • Performance
  • PostgreSQL
  • Productivity
  • Solidity
  • Vue.js
  • Web
  • Wireless
  • 🐧 How Linux Works (notes)
  • 🐧 Linux Kernel Comments (notes)
  • 🐧 Linux Kernel Development (notes)
  • 🐤 μc/OS-II Source Code (notes)
  • ☕ Understanding the JVM (notes)
  • ⛸️ Redis Implementation (notes)
  • 🗜️ Understanding Nginx (notes)
  • ⚙️ Netty in Action (notes)
  • ☁️ Spring Microservices (notes)
  • ⚒️ The Annotated STL Sources (notes)
  • ☕ Java Development Kit 8
GitHub
  • 📝 Notes
    • Algorithm
      • Algorithm - Bloom Filter
      • Algorithm - Disjoint Set
      • Algorithm - Fast Power
      • Algorithm - KMP
      • Algorithm - Monotonic Stack
      • Algorithm - RB-Tree
      • Algorithm - Regular Expression
      • Algorithm - Sliding Window
      • Online Judge - I/O
    • C++
      • C++ - Const
      • C++ File I/O
      • C++ - Object Layout
      • C++ - Operator Overload
      • C++ - Polymorphism
      • C++ STL algorithm
      • C++ STL map
      • C++ STL multimap
      • C++ STL priority_queue
      • C++ STL set
      • C++ STL string
      • C++ STL unordered_map
      • C++ STL vector
      • C++ - Smart Pointer
      • C++ - Template & Genericity
    • Compiler
      • ANTLR - Basic
      • Compiler - LLVM Architecture
      • Compiler - Multi-version GCC
    • Cryptography
      • Cryptography - Certbot
      • Cryptography - Digital Signature & PKCS #7
      • Cryptography - GPG
      • Cryptography - JWT
      • Cryptography - Keystore & Certificates
      • Cryptography - OAuth 2.0
      • Cryptography - Java 实现对称与非对称加密算法
      • Cryptography - TLS
    • DevOps
      • DevOps - Travis CI
    • Docker
      • Docker - Image & Storage Management
      • Docker - Image
      • Docker - Libcontainer
      • Docker - Multi-Arch Image
      • Docker - Multi-Stage Build
      • Docker - Network
      • Docker - Orchestration & Deployment
      • Docker - Overview
      • Docker - Service Building
      • Docker - Volume & Network Usage
      • Docker - Volume
      • Linux - Control Group
      • Linux - Namespace
    • Git
      • Git - Branch & Merge
      • Git - Cached
      • Git - Cherry Pick
      • Git - Commit
      • Git - Patch
      • Git - Proxy
      • Git - Rebase
      • Git - Reset
      • Git - Stash
      • Git - Theme for Git-Bash
    • Java
      • JVM - Synchronized
      • JVM - Volatile
      • Java - Annotation 注解
      • Java - BIO & NIO
      • Java - Class Path
      • Java - Condition and LockSupport
      • Java - Current Timestamp
      • Java - Deep Copy
      • Java - 运行环境配置
      • Java - Equals
      • Java - Exporting JAR
      • Java - Javadoc
      • Java - Lock
      • Java - Maven 项目构建工具
      • Java - References
      • Java - Reflection Mechanism
      • Java - String Split
      • Java - Thread Pool
      • Java - Thread
      • Tomcat - Class Loader
      • Tomcat - Container
    • Linux
      • addr2line
      • cut
      • df
      • du
      • fallocate
      • find
      • fio
      • grep
      • groupadd
      • gzip
      • head / tail
      • hexdump
      • iostat
      • iotop
      • kill
      • ldd
      • lsof
      • ltrace / strace
      • mpstat
      • netstat
      • nm
      • pidstat
      • pmap
      • readlink
      • readlink
      • rpm2cpio / rpm2archive
      • sort
      • tee
      • uniq
      • useradd
      • usermod
      • watch
      • wc
      • which
      • xargs
    • MS Office
      • MS Office - Add-in Dev
      • MS Office - Application
    • MySQL
      • InnoDB - Architecture
      • InnoDB - Backup
      • InnoDB - Checkpoint
      • InnoDB - Critical Features
      • InnoDB - Files
      • InnoDB - Index
      • InnoDB - Insert Buffer
      • InnoDB - Lock
      • InnoDB - Partition Table
      • InnoDB - Table Storage
      • MySQL - Server Configuration
      • MySQL - Storage Engine
    • Network
      • Network - ARP
      • Network - FTP
      • Network - GitHub Accelerating
      • HTTP - Message Format
      • HTTP - POST 提交表单的两种方式
      • Network - Proxy Server
      • Network - SCP
      • Network - SSH
      • Network - TCP Congestion Control
      • Network - TCP Connection Management
      • Network - TCP Flow Control
      • Network - TCP Retransmission
      • Network - Traceroute
      • Network - V2Ray
      • Network - WebSocket
      • Network - Windows 10 Mail APP
      • Network - frp
    • Operating System
      • Linux - Kernel Compilation
      • Linux - Multi-OS
      • Linux - Mutex & Condition
      • Linux - Operations
      • Linux: Package Manager
      • Linux - Process Manipulation
      • Linux - User ID
      • Linux - Execve
      • OS - Compile and Link
      • OS - Dynamic Linking
      • OS - ELF
      • Linux - Image
      • OS - Loading
      • OS - Shared Library Organization
      • OS - Static Linking
      • Syzkaller - Architecture
      • Syzkaller - Description Syntax
      • Syzkaller - Usage
      • Ubuntu - Desktop Recover (Python)
      • WSL: CentOS 8
    • Performance
      • Linux Performance - Perf Event
      • Linux Performance - Perf Record
      • Linux Performance - Perf Report
      • Linux Performance - Flame Graphs
      • Linux Performance - Off CPU Analyze
    • PostgreSQL
      • PostgreSQL - ANALYZE
      • PostgreSQL - Atomics
      • PostgreSQL - CREATE INDEX CONCURRENTLY
      • PostgreSQL - COPY FROM
      • PostgreSQL - COPY TO
      • PostgreSQL - Executor: Append
      • PostgreSQL - Executor: Group
      • PostgreSQL - Executor: Limit
      • PostgreSQL - Executor: Material
      • PostgreSQL - Executor: Nest Loop Join
      • PostgreSQL - Executor: Result
      • PostgreSQL - Executor: Sequential Scan
      • PostgreSQL - Executor: Sort
      • PostgreSQL - Executor: Unique
      • PostgreSQL - FDW Asynchronous Execution
      • PostgreSQL - GUC
      • PostgreSQL - Locking
      • PostgreSQL - LWLock
      • PostgreSQL - Multi Insert
      • PostgreSQL - Plan Hint GUC
      • PostgreSQL - Process Activity
      • PostgreSQL - Query Execution
      • PostgreSQL - Spinlock
      • PostgreSQL - Storage Management
      • PostgreSQL - VFD
      • PostgreSQL - WAL Insert
      • PostgreSQL - WAL Prefetch
    • Productivity
      • LaTeX
      • Venn Diagram
      • VuePress
    • Solidity
      • Solidity - ABI Specification
      • Solidity - Contracts
      • Solidity - Expressions and Control Structures
      • Solidity - Layout and Structure
      • Solidity - Remix IDE
      • Solidity - Slither
      • Solidity - Types
      • Solidity - Units and Globally Available Variables
    • Vue.js
      • Vue.js - Environment Variable
    • Web
      • Web - CORS
      • Web - OpenAPI Specification
    • Wireless
      • Wireless - WEP Cracking by Aircrack-ng
      • Wireless - WPS Cracking by Reaver
      • Wireless - wifiphisher

C++ - Operator Overload

Created by : Mr Dk.

2021 / 03 / 05 20:14

Nanjing, Jiangsu, China


About

运算符重载是一种形式的 C++ 多态,根据操作数的 数目 和 类型 来决定采用哪种操作。其一般形式为:

operatorop(args)

其中,后一个 op 可被替换为 C++ 中已经存在的且可以被重载的运算符,比如加法运算符 +:

operator+(args)

重载后,编译器能够对运算符进行等价的替换:

A = B + C;
A = B.operator+(C);

上述运算符重载方式只能由运算符左侧的操作数通过 成员函数 实现。另外可以通过 友元函数 实现运算符右侧操作数的运算符重载。

Limits

C++ 的运算符重载又如下限制:

  1. 运算符重载至少要有一个操作数是用户定义的类型 (防止用户为原生类型重载运算符)
  2. 不能违反运算符的句法规则 (原来是双目,那就还得是双目)
  3. 不能修改运算符优先级
  4. 不能创建新的运算符
  5. 不能重载如下运算符:
    • sizeof
    • .
    • .*
    • ::
    • ?:
    • typeid
    • const_cast
    • dynamic_cast
    • reinterpret_cast
    • static_cast
  6. 某些运算符只能以成员函数的形式重载 (不能以友元函数):
    • =
    • ()
    • []
    • ->

Implementation

Member Function

以成员函数实现运算符重载,默认了运算符左侧的操作数是当前对象,因此只需要传递一个右操作数作为参数即可。编译器负责将重载后的运算符替换为成员函数调用:

T::T operator*(const double &d) {
    // ...
}
A = B * 2.75;
A = B.operator*(2.75);

那么,这种情况怎么办呢?

A = 2.75 * B;

Friend Function

如果说将运算符的重载不重载为成员函数,而是重载为一个普通函数,通过传入两个参数,就可以实现按照需要获取操作数顺序。

T operator*(const double &d, T obj) {
    // ...
}
A = 2.75 * B;
A = operator*(2.75, B);

这时出现了一个问题:如果这个函数的实现需要访问类的 私有成员变量 该怎么办?由于非成员函数不是由对象调用,因此不能访问类内部的私有成员变量。这时候就需要类的 友元函数 出马。类的友元函数是非成员函数,但是访问权限与成员函数相同 - 可以访问类的私有成员变量。

创建友元函数的方式是将函数声明在类内,并在原型前加上 friend 关键字 (不需要类限定符 ::):

friend T operator*(const double &d, T obj) {
    // T.xxx;
}

总结,如果要为类重载运算符,并且不是用类对象作为第一个参数,可以使用友元函数反转操作数顺序。当然,具体用不用友元取决于 是否要访问类内的私有成员变量:如果只是将对象作为一个整体使用,不用友元也一样。

Example: Overload <<

对于一个自定义的类,用户希望能够通过 cout << obj 直接打印对象信息。对于该运算符来说,显然用户不会去修改 iostream 的头文件来为 cout 对象重载 << 运算符。所以,需要通过友元 (如果需要打印类内私有变量的值) 的方式为该类对象重载 << 运算符。

friend void operator<<(ostream &os, const T &t) {
    os << t.xxx;
}

由于要操作 cout 对象本身,所以这里传入的参数是引用。编译器会将代码转换为:

cout << obj;
operator<<(cout, obj);

但是这样还是有个问题:没法适用于连续的 << 运算符:

cout << "Hello" << obj << "hhh";
// (cout << "Hello") << obj << "hhh";
// cout << obj << "hhh";
// (void) << "hhh";

所以重载函数的返回值也应当是 ostream 对象,并且是一个引用:

friend ostream & operator<<(ostream &os, const T &t) {
    os << t.xxx;
    return os;
}

这样就可以实现如下的效果:

cout << "Hello" << obj << "hhh";
// (cout << "Hello") << obj << "hhh";
// cout << obj << "hhh";
// cout << "hhh";

Example: STL

由于 STL 中的容器基本实现了泛型,因此用户可以将自定义类型的对象放进 STL 容器中。在对 STL 容器内元素进行排序时,算法默认使用 < 运算符。因此,如果要对自定义类型的对象进行排序,需要为类重载 < 运算符。函数原型如下:

bool operator<(const T &t) const;

这里为什么要使用 const 呢?STL 底层的比较函数实现类似如下:

bool operator<(const T &__x, const T &__y) {
    return __x < __y;
}

只有 const 函数才可以访问 const 对象中的数据。

Edit this page on GitHub
Prev
C++ - Object Layout
Next
C++ - Polymorphism