C++操作符重载案例

        在学习ZLToolKit源码时,发现代码中涉及好多运算符重载,因此对其做一下归类学习。

        直接写一个代码案例如下:

#include <iostream>
#include <memory>
#include <functional>

// 定义类 A
class A {
public:
    A(int a) { _a = a;  }

    // 重载()
    std::string operator()(std::string s) {
        std::cout << "-----operator () -----" << std::endl;
        return s;
    }

    // 重载bool类型
    operator bool() {
        std::cout << "-----operator bool -----" << std::endl;
        return _a > 0;
    }

    // 重载int类型
    operator int() {
        std::cout << "-----operator int -----" << std::endl;
        return _a;
    }

    // 重载=运算法,入参(被赋值类型int)
    A operator= (int a) {
        std::cout << "-----operator =(int) -----" << std::endl;
        _a = a;
        return *this;
    }

    // 重载=运算法,入参(被赋值类型A)
    A operator= (A a) {
        std::cout &l
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值