C++运算符重载大全

  重载运算符后就可以将自己定义的类用于各种运算,如数学运算符+-*/,以下列出各类运算符重载示例,先上完整代码

class OperatorTest
{
public:
    int data;
    //双目算术运算符 +,-,*,/,%
    int operator+(const OperatorTest& other)
    {
        int tmp = data + other.data;
        std::cout << "operator + called return=" << tmp<< std::endl;
        return tmp;
    }
    int operator-(const OperatorTest& other)
    {
        int tmp = data - other.data;
        std::cout << "operator - called return=" << tmp << std::endl;
        return tmp;
    }
    int operator*(const OperatorTest& other)
    {
        int tmp = data * other.data;
        std::cout << "operator * called return=" << tmp << std::endl;
        return tmp;
    }
    int operator/(const OperatorTest& other)
    {
        int tmp = data/other.data;
        std::cout << "operator / called return=" << tmp << std::endl;
        return tmp;
    }
    int operator%(const OperatorTest& other)
    {
        int tmp = data%other.data;
        std::cout << "operator % called return=" << tmp << std::endl;
        return tmp;
    }

    //关系运算符 ==,!=,<,>,<=,>=
    bool operator==(const OperatorTest& other)
    {
        bool tmp = data == other.data;
        std::cout << "operator == called return=" << tmp << std::endl;
        return tmp;
    }
    bool operator!=(const OperatorTest& other)
    {
        bool tmp = data != other.data;
        std::cout << "operator != called return=" << tmp << std::endl;
        return tmp;
    }
    bool operator>(const OperatorTest& other)
    {
        bool tmp = data > other.data;
        std::cout << "operator > called return=" << tmp << std::endl;
        return tmp;
    }
    bool operator<(const OperatorTest& other)
    {
        bool tmp = data < other.data;
        std::cout << "operator < called return=" << tmp << std::endl;
        return tmp;
    }
    bool operator>=(const OperatorTest& other)
    {
        bool tmp = data >= other.data;
        std::cout << "operator >= called return=" << tmp << std::endl;
        return tmp;
    }
    bool operator<=(const OperatorTest& other)
    {
        bool tmp = data <= other.data;
        std::cout << "operator <= called return=" << tmp << std::endl;
        return tmp;
    }

    //逻辑运算符 ||,&&,!
    bool operator ||(const OperatorTest& other)
    {
        bool tmp = data || other.data;
        std::cout << "operator || called return=" << tmp << std::endl;
        return tmp;
    }
    bool operator &&(const OperatorTest& other)
    {
        bool tmp = data && other.data;
        std::cout << "operator && called return=" << tmp << std::endl;
        return tmp;
    }
    bool operator!()
    {
        bool tmp = data ==0;
        std::cout << "operator ! called return=" << tmp << std::endl;
        return tmp;
    }

    //位运算 &,|,~,^,<<,>>
    int operator&(const OperatorTest& other)
    {
        int tmp = data & other.data;
        std::cout << "operator & called return=" << tmp << std::endl;
        return tmp;
    }
    int operator|(const OperatorTest& other)
    {
        int tmp = data | other.data;
        std::cout << "operator | called return=" << tmp << std::endl;
        return tmp;
    }

    int operator~()
    {
        int tmp = ~data;
        std::cout << "operator ~ called return=" << tmp << std::endl;
        return tmp;
    }
    int operator^(const OperatorTest& other)
    {
        int tmp = data ^ other.data;
        std::cout << "operator ^ called return=" << tmp << std::endl;
        return tmp;
    }
    int operator<<(int i)
    {
        int tmp = data << i;
        std::cout << "operator << called return=" << tmp << std::endl;
        return tmp;
    }
    int operator>>(int i)
    {
        int tmp = data >> i;
        std::cout << "operator >> called return=" << tmp << std::endl;
        return tmp;
    }

    //赋值运算符 =,+=,-=,*=,/=,%=,&=,|=,^=,<<=,>>=
    const OperatorTest& operator=(const OperatorTest& other)
    {
        data = other.data;
        std::cout << "operator = called data=" << data << std::endl;
        return *this;
    }
    const OperatorTest& operator+=(const OperatorTest& other)
    {
        data += other.data;
        std::cout << "operator += called data=" << data << std::endl;
        return *this;
    }
    const OperatorTest& operator-=(const OperatorTest& other)
    {
        data -= other.data;
        std::cout << "operator -= called data=" << data << std::endl;
        return *this;
    }
    const OperatorTest& operator*=(const OperatorTest& other)
    {
        data *= other.data;
        std::cout << "operator *= called data=" << data << std::endl;
        return *this;
    }
    const OperatorTest& operator/=(const OperatorTest& other)
    {
        data /= other.data;
        std::cout << "operator /= called data=" << data << std::endl;
        return *this;
    }
    const OperatorTest& operator%=(const OperatorTest& other)
    {
        data %= other.data;
        std::cout << "operator %= called data=" << data << std::endl;
        return *this;
    }
    const OperatorTest& operator&=(const OperatorTest& other)
    {
        data &= other.data;
        std::cout << "operator &= called data=" << data << std::endl;
        return *this;
    }
    const OperatorTest& operator|=(const OperatorTest& other)
    {
        data |= other.data;
        std::cout << "operator |= called data=" << data << std::endl;
        return *this;
    }
    const OperatorTest& operator^=(const OperatorTest& other)
    {
        data ^= other.data;
        std::cout << "operator ^= called data=" << data << std::endl;
        return *this;
    }
    const OperatorTest& operator<<=(int i)
    {
        data <<= i;
        std::cout << "operator <<= called data=" << data << std::endl;
        return *this;
    }
    const OperatorTest& operator>>=(int i)
    {
        data >>= i;
        std::cout << "operator >>= called data=" << data << std::endl;
        return *this;
    }

    //单目运算符 +(正),-(负),*(取值),&(取地址),++,--
    //++,--有前缀和后缀两种情况
    int operator-()
    {
        int tmp = -data;
        std::cout << "operator - called return=" << tmp << std::endl;
        return tmp;
    }
    int operator+()
    {
        int tmp = data;
        std::cout << "operator + called return=" << tmp << std::endl;
        return tmp;
    }
    int operator*()
    {
        int tmp = *(&data);//取值操作,这里直接返回data
        std::cout << "operator * called return=" << tmp << std::endl;
        return tmp;
    }
    unsigned long operator&()
    {
        unsigned long tmp = (unsigned long)this;//返回地址
        std::cout << "operator & called return=" << tmp << std::endl;
        return tmp;
    }
    int operator++()//前缀自增
    {
        int tmp = ++data;
        std::cout << "operator ++ called return=" << tmp << std::endl;
        return tmp;
    }
    int operator++(int)//后缀自增
    {
        int tmp = data++;
        std::cout << "operator ++ called return=" << tmp << std::endl;
        return tmp;
    }
    int operator--()//前缀自减
    {
        int tmp = --data;
        std::cout << "operator -- called return=" << tmp << std::endl;
        return tmp;
    }
    int operator--(int)//后缀自减
    {
        int tmp = data--;
        std::cout << "operator -- called return=" << tmp << std::endl;
        return tmp;
    }

	//内存分配 new,delete,new[],delete[]
	//调用全局的::operator new和::operator delete分配内存
	void* operator new(size_t size)
	{
		std::cout << "operator new called"<< std::endl;
		return ::operator new(size);
	}
	void operator delete(void* ptr,size_t size)
	{
		std::cout << "operator delete called" << std::endl;
		::operator delete(ptr,size);
	}
	void* operator new[](size_t size)
	{
		std::cout << "operator new[] called" << std::endl;
		return ::operator new(size);
	}
	void operator delete[](void* ptr,size_t size)
	{
		std::cout << "operator delete[] called" << std::endl;
		::operator delete[](ptr,size);
	}

    //函数运算符 ()
    //函数运算符可不带参数,或带多个参数
    void operator ()()
    {
        data = 0;
        std::cout << "operator () called data=" <<data<< std::endl;
    }
    void operator ()(int i)
    {
        data = i;
        std::cout << "operator (i) called data=" << data << std::endl;
    }

    //成员访问运算符->
    OperatorTest* operator->()
    {
        std::cout << "operator -> called" << std::endl;
        return this;
    }

    //下标运算符[]
    int operator[](int i)
    {
        std::cout << "operator [] called return=" <<data<< std::endl;
        return data;
    }

    //逗号运算符 ,
    //逗号运算符不重载也可以使用,但是返回的是','后面的值,在这个重载运算中返回data
    int operator ,(int i)
    {
        std::cout << "operator , called return=" << data << std::endl;
        return data;
    }

    //类型转换运算符
    explicit operator bool()
    {
        bool tmp = data > 0;
        std::cout << "operator bool called return=" << tmp << std::endl;
        return tmp;
    }
    explicit operator char()
    {
        char tmp = data>>24;
        std::cout << "operator char called return=" << (int)tmp << std::endl;
        return tmp;
    }
};

为了说明重载函数都被调用了,信息打印都放在重载函数里,外部不打印,接下来分成几个部分进行测试

1、算术运算符

void TestOperator1()
{
    OperatorTest ot1;
    ot1.data = 5;
    OperatorTest ot2;
    ot2.data = -5;
    ot1 + ot2;
    ot1 - ot2;
    ot1 * ot2;
    ot1 / ot2;
    ot1 % ot2;
}

运行结果

2、比较运算符

void TestOperator2()
{
    OperatorTest ot1;
    ot1.data = 5;
    OperatorTest ot2;
    ot2.data = 5;
    ot1 == ot2;
    ot1 != ot2;
    ot1 > ot2;
    ot1 < ot2;
    ot1 >= ot2;
    ot1 <= ot2;
}

运行结果

3、逻辑运算符

void TestOperator3()
{
    OperatorTest ot1;
    ot1.data = 5;
    OperatorTest ot2;
    ot2.data = 0;
    ot1&&ot2;
    ot1 || ot2;
    !ot1;
}

运行结果

4、位运算符

void TestOperator4()
{
    OperatorTest ot1;
    ot1.data = 7;
    OperatorTest ot2;
    ot2.data = 8;
    ot1&ot2;
    ot1|ot2;
    ~ot1;
    ot1^ot2;
    ot1<<2;
    ot1 >> 1;
}

运行结果

5、赋值运算符

void TestOperator5()
{
    OperatorTest ot1;
    ot1.data = 5;
    OperatorTest ot2;
    ot2 = ot1;
    ot2 += ot1;
    ot2 -= ot1;
    ot2 *= ot1;
    ot2 <<= 1;
    ot2 >>= 2;
    ot2 /= ot1;
    ot2 %= ot1;
    ot2 &= ot1;
    ot2 |= ot1;
    ot2 ^= ot1;
}

运行结果

6、单目运算符

void TestOperator6()
{
    OperatorTest ot1;
    ot1.data = 7;
    +ot1;
    -ot1;
    *ot1;
    &ot1;
    ++ot1;
    ot1++;
    --ot1;
    ot1--;
}

运行结果

7、内存管理运算符

这里重载的operator new和operator delete其实是重载了全局::operator new和::operator delete内存管理运算符,而不是C++中的new和delete操作符,在下面测试例子中的new操作符中调用了这里重载的operator new,同时调用OperatorTest的构造函数,delete同理。了解更多可以参考https://blog.csdn.net/yb0022/article/details/98386260

void TestOperator7()
{
    OperatorTest* ptr1 = new OperatorTest;
    delete ptr1;
    OperatorTest* ptr2 = new OperatorTest[3];
    delete[] ptr2;
}

运行结果

8、其他运算符

void TestOperator8()
{
    OperatorTest ot1;
    ot1();           //不带参数的函数重载符
    ot1(5);          //带一个int参数的函数重载符
    ot1->data;       //重载'->'后即使不是指针也可以使用->访问成员
    ot1[0];          //即使不是指针或数组也可以调用[]运算符
    int j = (ot1, 0);//如果不重载','则j=0,重载之后j=5
}

运行结果

9、类型转换运算符

类型转换运算符名称就是需要转换的类型名,如bool char int,不需要声明返回类型,写成operator bool(){}这种类型就可以,如果在前面加上explicit关键字,表示必须是显式转换(bool b=(bool)ot1),不能是隐式转换(bool b=ot1),否则编译通不过,这里实现了bool和char两种类型转换,其中char转换是取data的最高位字节。

void TestOperator9()
{
    OperatorTest ot1;
    ot1.data = 0xff000000;
    bool b = (bool)ot1;//可直接用于if语句if(ot1){}
    char c = (char)ot1;
}

ot1.data最高字节是0xff,是负数,所以bool转换后b为false,char转换后c为0xff, 也就是-1

10、全局运算符重载

上面的运算符都重载为成员方法,其中很多运算符可以重载为全局方法,只是参数不一样。如果希望OperatorTest类也想字符串一样被cout输出到屏幕,可以像下面一样重载'<<'运算符(全局),这样就可以自己决定如何输出到屏幕。

std::ostream& operator<<(std::ostream& os,OperatorTest ot)
{
    os << "data="<<ot.data;
    return os;
}

void TestOperator10()
{
    OperatorTest ot1;
    ot1.data = 128;
    std::cout << ot1 << std::endl;
}

注意:

(1)运算符重载中有的以参数个数来区分运算符类型,如'+' '-' '*' '&' '++' '--',++和--用参数个数区分是前缀还是后缀运算符,并未用到参数的值;有的参数个数可变,如函数运算符'()';其它的参数个数均不能变。
(2)new和new[]第一个参数必须是size_t类型,后面可以再加其他任意参数,只能返回void*类型;delete和delete[]第一个参数类型必须是void*,且必须返回void,即没有返回值;其它的参数类型或返回类型可变。
(3)运算符优先级不会改变。
(4)=,[],(),->必须重载为成员函数。
(5)::,.,.*,sizeof,?:,#,typeid运算符不能重载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值