news 2026/6/6 5:50:22

干货分享|C++运算符重载知识点

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
干货分享|C++运算符重载知识点

重载赋值运算符与构造配对出现

C++ 非常重要的两个构造,拷贝构造和移动构造。

分别对应拷贝赋值和移动赋值。请同时出现让外部人员认为行为的一致。

class SimpleVector { private: int m_size; int* m_data; public: SimpleVector(int len) : m_size(len), m_data(new int[len]{}) { } ~SimpleVector() { if (m_data != nullptr) { delete[] m_data; m_data = nullptr; m_size = 0; } } public: // 拷贝 SimpleVector(const SimpleVector& rhs) { this->m_size = rhs.m_size; this->m_data = new int[this->m_size]; for (int i = 0; i < this->m_size; i += 1) { this->m_data[i] = rhs.m_data[i]; } } SimpleVector& operator=(const SimpleVector& rhs) { this->~SimpleVector(); this->m_size = rhs.m_size; this->m_data = new int[this->m_size]; for (int i = 0; i < this->m_size; i += 1) { this->m_data[i] = rhs.m_data[i]; } return *this; } // 移动 SimpleVector(SimpleVector&& rhs) { this->m_size = rhs.m_size; this->m_data = rhs.m_data; rhs.m_size = 0; rhs.m_data = nullptr; } SimpleVector& operator=(SimpleVector&& rhs) { this->m_size = rhs.m_size; this->m_data = rhs.m_data; rhs.m_size = 0; rhs.m_data = nullptr; return *this; } };

重载 operator new 和 delete 配对出现

就像是在工厂模式中一样,有构造工厂的同时要提供析构工厂。

class Node { public: int x; Node(int x) : x(x) { } public: static void* operator new(size_t size) { // 调用全局的 operator new return ::operator new(size); } static void operator delete(void* ptr) { // 调用全局的 ::operator delete ::operator delete(ptr); } };

考虑const 左右值重载

一般对应左右值重载的情况不多,此处以 const 重载为例。

class SimpleVector { private: int m_size; int* m_data; public: SimpleVector(int len) : m_size(len), m_data(new int[len]{}) { } ~SimpleVector() { if (m_data != nullptr) { delete[] m_data; m_data = nullptr; m_size = 0; } } public: // const 版本重载 const int& operator[](int index) const { return m_data[index]; }

谨慎返回静态对象

使用静态对象可以减少临时对象的生成,但是提升效率的同时往往会造成许多意想不到的问题。

#include <iostream> class Node { public: int x; Node(int x) : x(x) { } public: bool operator==(const Node& rhs) { std::cout << "this=" << this << " rhs=" << &rhs << std::endl; return this->x == rhs.x; } }; Node& operator+(const Node& lhs, const Node& rhs) { static Node pub(0); pub.x = lhs.x + rhs.x; return pub; } int main() { Node a(10); Node b(20); Node c(30); Node d(40); // 减少了临时对象的生成 a + b; c + d; // 不是我们理想的效果

谨慎重载类型

C++ 支持类型的重载,使得外部在调用对象的时候能够在某些情况下达到使用类似目标类型的效果。

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/6 5:49:47

从GPT-2到GDPR:NLP工程师必须了解的5个伦理实战问题(含避坑清单)

从GPT-2到GDPR&#xff1a;NLP工程师必须了解的5个伦理实战问题&#xff08;含避坑清单&#xff09;当NLP技术从实验室走向真实世界&#xff0c;算法工程师们突然发现自己站在了伦理与技术的十字路口。去年某招聘平台因AI简历筛选系统涉嫌性别歧视被起诉的案例&#xff0c;给行…

作者头像 李华
网站建设 2026/6/6 5:40:37

Blurable扩展开发指南:如何为自定义UIView组件添加模糊功能

Blurable扩展开发指南&#xff1a;如何为自定义UIView组件添加模糊功能 【免费下载链接】Blurable Apply a Gaussian Blur to any UIView with Swift Protocol Extensions 项目地址: https://gitcode.com/gh_mirrors/bl/Blurable 想要为你的iOS应用添加优雅的毛玻璃效果…

作者头像 李华
网站建设 2026/6/6 5:37:18

告别裸奔!用CubeMX+Keil给STM32F407装上RTX5实时系统(保姆级避坑指南)

从裸机到RTOS&#xff1a;STM32F407实战RTX5实时系统全攻略第一次在STM32上尝试RTOS的感觉&#xff0c;就像给自行车装上涡轮增压——明明还是那台熟悉的硬件&#xff0c;却突然获得了全新的能力维度。三年前接手一个工业传感器项目时&#xff0c;我还在用状态机在while循环里苦…

作者头像 李华
网站建设 2026/6/6 5:35:40

Windows电脑安装安卓应用,这3个技巧让你效率翻倍!

Windows电脑安装安卓应用&#xff0c;这3个技巧让你效率翻倍&#xff01; 【免费下载链接】APK-Installer An Android Application Installer for Windows 项目地址: https://gitcode.com/GitHub_Trending/ap/APK-Installer 你是否曾经想在Windows电脑上运行安卓应用&am…

作者头像 李华