干货分享|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++ 支持类型的重载,使得外部在调用对象的时候能够在某些情况下达到使用类似目标类型的效果。

相关推荐
程序员天天困6 分钟前
优雅记录操作日志:从注解到 SpEL 的全链路实践与开源方案对比
java·后端
战血石LoveYY24 分钟前
Integer类型超限,导致数据异常
java·算法
会周易的程序员25 分钟前
从零构建多核CPU负载自适应控制系统
linux·c++·笔记·物联网·测试工具
紫金修道34 分钟前
PnP算法介绍(Perspective-n-Point)
算法
程序猿编码1 小时前
用C++从零开始造一个微型GPT,不借助任何第三方库
开发语言·c++·gpt·模型推理
qz5zwangzihan11 小时前
题解:Atcoder Beginner Contest abc466 F - Many Mod Calculation
c++·题解·优先队列·atcoder·大根堆·abc466·abc466f
乐观的Terry2 小时前
1、为什么要自己造发布系统
java·spring boot·后端·spring cloud·架构
froyoisle2 小时前
CSP 真题解析:[CSP-J 2019-T4] 加工零件
c++·算法·bfs·csp-j·算法竞赛·信息学·信奥赛
2401_869769592 小时前
内容7 内存管理 1
c++
兰令水2 小时前
hot100【acm版】【2026.7.13打卡-java版本】
java·开发语言·数据结构·算法·leetcode·面试