c++操作符重载函数使用

文章介绍了如何在C++中为自定义类如Vector、Person、Matrix、MyArray和Counter重载加法、比较、赋值、下标、递增和递减等操作符,以实现类似内置类型的使用方式,提升代码的可读性和表达力。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

操作符重载函数允许自定义类或结构体的实例使用特定的操作符,就像内置类型一样。以下是一些常见的操作符重载函数的例子:

  1. 重载加法操作符

    class Vector {
    public:
        int x, y;
        Vector(int x = 0, int y = 0) : x(x), y(y) {}
    
        Vector operator+(const Vector& other) const {
            return Vector(x + other.x, y + other.y);
        }
    };
    
    int main() {
        Vector v1(1, 2);
        Vector v2(3, 4);
        Vector v3 = v1 + v2; // 使用重载的加法操作符
        return 0;
    }
    
  2. 重载比较操作符

    class Person {
    public:
        std::string name;
        int age;
        Person(std::string n, int a) : name(n), age(a) {}
    
        bool operator==(const Person& other) const {
            return name == other.name && age == other.age;
        }
    };
    
    int main() {
        Person p1("Alice", 30);
        Person p2("Alice", 30);
        bool same = (p1 == p2); // 使用重载的比较操作符
        return 0;
    }
    
  3. 重载赋值操作符

    class Matrix {
    public:
        // 假设Matrix有足够的成员变量来存储矩阵数据
        Matrix operator=(const Matrix& other) {
            // 自我赋值检查
            if (this == &other) {
                return *this;
            }
            // 赋值操作
            // ...
            return *this;
        }
    };
    
    int main() {
        Matrix m1, m2;
        m1 = m2; // 使用重载的赋值操作符
        return 0;
    }
    
  4. 重载下标操作符

    class MyArray {
    private:
        int* data;
        size_t size;
    public:
        MyArray(size_t s) : size(s) {
            data = new int[s];
            for (size_t i = 0; i < s; ++i) {
                data[i] = 0;
            }
        }
        ~MyArray() {
            delete[] data;
        }
    
        int& operator[](size_t index) {
            return data[index];
        }
        const int& operator[](size_t index) const {
            return data[index];
        }
    };
    
    int main() {
        MyArray arr(10);
        arr[0] = 42; // 使用重载的下标操作符
        int value = arr[0]; // 同时重载了const版本的下标操作符
        return 0;
    }
    
  5. 重载递增和递减操作符

    class Counter {
    public:
        int value;
        Counter(int start = 0) : value(start) {}
    
        Counter& operator++() { // 前置递增
            ++value;
            return *this;
        }
        Counter operator++(int) { // 后置递增
            Counter tmp = *this;
            ++value;
            return tmp;
        }
        Counter& operator--() { // 前置递减
            --value;
            return *this;
        }
        Counter operator--(int) { // 后置递减
            Counter tmp = *this;
            --value;
            return tmp;
        }
    };
    
    int main() {
        Counter c;
        ++c; // 使用前置递增操作符
        c++; // 使用后置递增操作符
        c--; // 使用前置递减操作符
        c--(1); // 使用后置递减操作符
        return 0;
    }
    

这些例子展示了如何为自定义类型重载各种操作符,使得它们的行为与内置类型类似,从而提高代码的可读性和表达能力。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值