c++运算符重载的一些理解

运算符重载类似于调用函数,对于用户定义的类,c++没有可以比较他们关系的运算符,所以就像定义函数那样来重载运算符。

#include <iostream>
using namespace std;
class Tricycle
{
public:
    Tricycle();
    // copy constructor and destructor use default
    int getSpeed() const { return *speed; }
    void setSpeed(int newSpeed) { *speed = newSpeed; }
    //Tricycle operator=(const Tricycle&);
    bool operator==(const Tricycle&);           //congzai==
  
private:
    int *speed;
};
  
Tricycle::Tricycle()
{
    speed = new int;
    *speed = 5;
}

bool Tricycle::operator==(const Tricycle& rhs)
{
    if (this==&rhs)
    {
        return true;
    }
    if(this->getSpeed()>=rhs.getSpeed())    
    {
        return true;                  //if getSpeed()>=rhs.getSpeed()  return ture
    }
    return false;
}
  
/*Tricycle Tricycle::operator=(const Tricycle& rhs)
{
    if (this == &rhs)
        return *this;
    delete speed;
    speed = new int;
    *speed = rhs.getSpeed();
    return *this;
}*/
  
int main()
{
    Tricycle wichita;
    std::cout << "Wichita's speed: " << wichita.getSpeed() 
        << "\n";
    std::cout << "Setting Wichita's speed to 8 ...\n";
    wichita.setSpeed(8);
    Tricycle dallas;
    std::cout << "Dallas' speed: " << dallas.getSpeed() 
        << "\n";
    std::cout << "Setting Dallass speed to 7 ...\n";
    dallas.setSpeed(7);
    cout<<"wichita.getSpeed= "<<wichita.getSpeed()<<endl;

    cout<<"dallas.getSpeed= "<<dallas.getSpeed()<<endl;
    if(wichita == dallas)
    {
        cout<<"Wichita speed>=Dallass speed\n";   //houmiande xiao fanhui ture
    }
    else
    {
        cout<<"Wichita speed < Dallas speed\n";
    }


    return 0;
}

其中,就像函数那样,此处是重载==运算符,但是并非是判断相等的,而是判断两个对象中的speed成员变量是否是运算符==左侧的>=右侧的,返回一个bool类型,通过调用运算符,打印他们之间的关系。

下面是重载=运算符的例子:

#include <iostream>
  
class Tricycle
{
public:
    Tricycle();
    // copy constructor and destructor use default
    int getSpeed() const { return *speed; }
    void setSpeed(int newSpeed) { *speed = newSpeed; }
    Tricycle operator=(const Tricycle&);
  
private:
    int *speed;
};
  
Tricycle::Tricycle()
{
    speed = new int;
    *speed = 5;
}
  
Tricycle Tricycle::operator=(const Tricycle& rhs)
{
    if (this == &rhs)
        return *this;
    delete speed;
    speed = new int;
    *speed = rhs.getSpeed();
    return *this;
}
  
int main()
{
    Tricycle wichita;
    std::cout << "Wichita's speed: " << wichita.getSpeed() 
        << "\n";
    std::cout << "Setting Wichita's speed to 6 ...\n";
    wichita.setSpeed(6);
    Tricycle dallas;
    std::cout << "Dallas' speed: " << dallas.getSpeed() 
        << "\n";
    std::cout << "Copying Wichita to Dallas ...\n";
    wichita = dallas;
    std::cout << "Dallas' speed: " << dallas.getSpeed() 
        << "\n";
    return 0;
}

ps:内容来自c++入门经典的源代码

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Tech沉思录

点赞加投币,感谢您的资瓷~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值