万物可运算: 运算符重载,

为什么要使用运算符重载

C/C++的运算符,支持的数据类型,仅限于基本数据类型。

问题:一头牛+一头马 = ?(牛马神兽?)
一个圆 +一个圆 = ? (想要变成一个更大的圆)
一头牛 – 一只羊 = ? (想要变成4只羊,原始的以物易物:1头牛价值5只羊)

解决方案:
使用运算符重载
运算符重载的基本用法

方式1:使用成员函数重载运算符
需求:
// 规则:
// 一斤牛肉:2斤猪肉
// 一斤羊肉:3斤猪肉

Cow.h

#pragma once

class Pork;
class Goat;

class Cow
{
public:
	Cow(int weight);

	// 参数此时定义为引用类型,更合适,避免拷贝
	Pork operator+(const Cow& cow);  //同类型进行运算,很频繁

	Pork operator+(const Goat& goat); //不同类型进行运算,比较少见
private:
	int weight = 0;
};

Cow.cpp

#include "Cow.h"
#include "Pork.h"
#include "Goat.h"

Cow::Cow(int weight)
{
	this->weight = weight;
}


// 规则:
// 一斤牛肉:2斤猪肉
// 一斤羊肉:3斤猪肉
Pork Cow::operator+(const Cow &cow)
{
	int tmp = (this->weight + cow.weight) * 2;
	return Pork(tmp);
}

Pork Cow::operator+(const Goat& goat)
{
	// 不能直接访问goat.weight
	//int tmp = this->weight * 2 + goat.weight * 3;
	int tmp = this->weight * 2 + goat.getWeight() * 3;
	return Pork(tmp);
}

Goat.h

#pragma once
class Goat
{
public:
	Goat(int weight);
	int getWeight(void) const;
private:
	int weight = 0;
};

Goat.cpp

#include "Goat.h"

Goat::Goat(int weight) {
	this->weight = weight;
}

int Goat::getWeight(void) const
{
	return weight;
}

Pork.h

#pragma once
#include <iostream>

class Pork
{
public:
	Pork(int weight);
	std::string  description(void);

private:
	int weight = 0;
};

Pork.cpp

#include "Pork.h"
#include <sstream>

Pork::Pork(int weight)
{
	this->weight = weight;
}

std::string Pork::description(void)
{
	std::stringstream ret;
	ret << weight << "斤猪肉";
	return ret.str();
}

main.cpp

#include <iostream>
#include "Pork.h"
#include "Cow.h"
#include "Goat.h"

int main(void) {
	Cow c1(100);
	Cow c2(200);

	// 调用c1.operator+(c2);
	//相当于:Pork p = c1.operator+(c2);
	Pork p = c1 + c2;
	std::cout << p.description() << std::endl;

	Goat g1(100);
	p = c1 + g1;
	std::cout << p.description() << std::endl;
	
	system("pause");
	return 0;
}

两种方式的区别
区别:
1.使用成员函数来实现运算符重载时,少写一个参数,因为第一个参数就是this指针。

两种方式的选择:
1.一般情况下,单目运算符重载,使用成员函数进行重载更方便(不用写参数)
2.一般情况下,双目运算符重载,使用友元函数更直观
方便实现a+b和b+a相同的效果,成员函数方式无法实现。
例如: 100 + cow; 只能通过友元函数来实现
cow +100; 友元函数和成员函数都可以实现
特殊情况:
(1)= () [ ] -> 不能重载为类的友元函数!!!(否则可能和C++的其他规则矛盾),只能使用成员函数形式进行重载。
(2)如果运算符的第一个操作数要求使用隐式类型转换,则必须为友元函数(成员函数方式的第一个参数是this指针)

注意:
同一个运算符重载, 不能同时使用两种方式来重载,会导致编译器不知道选择哪一个(二义性)

运算符重载的禁区和规则

  1. 为了防止对标准类型进行运算符重载,
    C++规定重载运算符的操作对象至少有一个不是标准类型,而是用户自定义的类型
    比如不能重载 1+2
    但是可以重载 cow + 2 和 2 + cow // cow是自定义的对象

2.不能改变原运算符的语法规则, 比如不能把双目运算符重载为单目运算

3.不能改变原运算符的优先级

4.不能创建新的运算符,比如 operator**就是非法的, operator*是可以的

5.不能对以下这四种运算符,使用友元函数进行重载
= 赋值运算符,()函数调用运算符,[ ]下标运算符,->通过指针访问类成员

  1. 不能对禁止重载的运算符进行重载
    在这里插入图片描述

重载加减运算符=

在这里插入图片描述
注意:
注意赋值运算符重载的返回类型 和参数类型。
返回引用类型,便于连续赋值
参数使用应用类型, 可以省去一次拷贝
参数使用const, 便于保护实参不被破坏。

重载关系运算符>、<、==

Boy.h

#pragma once
#include <string>

class Boy
{
public:
	Boy(const char* name=NULL, int age=0, int salary=0, int darkHorse=0);
	~Boy();

	Boy& operator=(const Boy& boy);

	bool operator>(const Boy& boy);
	bool operator<(const Boy& boy);
	bool operator==(const Boy& boy);

	std::string description(void);
private:
	char* name;
	int age;
	int salary;
	int darkHorse; //黑马值,潜力系数
	unsigned int id; // 编号
	static int LAST_ID;

	int power() const; //综合能力值
};

Boy.cpp

#include "boy.h"
#include <string.h>
#include <sstream>

int Boy::LAST_ID = 0;  //初始值是0

Boy::Boy(const char* name, int age, int salary, int darkHorse)
{
	if (!name) {
		name = "未命名";
	}

	this->name = new char[strlen(name) + 1];
	strcpy_s(this->name, strlen(name)+1, name);

	this->age = age;
	this->salary = salary;
	this->darkHorse = darkHorse;
	this->id = ++LAST_ID;
}

Boy::~Boy()
{
	if (name) {
		delete name;
	}
}

Boy& Boy::operator=(const Boy& boy)
{
	if (name) {
		delete name;  //释放原来的内存
	}
	name = new char[strlen(boy.name) + 1]; //分配新的内存
	strcpy_s(name, strlen(boy.name)+1, boy.name);

	this->age = boy.age;
	this->salary = boy.salary;
	this->darkHorse = boy.darkHorse;
	//this->id = boy.id;  //根据需求来确定是否要拷贝id
	return *this;
}

bool Boy::operator>(const Boy& boy)
{
	// 设置比较规则:
	// 薪资 * 黑马系数 + (100-年龄)*100
	if (power() > boy.power()) {
		return true;
	}
	else {
		return false;
	}
}

bool Boy::operator<(const Boy& boy)
{
	if (power() < boy.power()) {
		return true;
	}
	else {
		return false;
	}
}

bool Boy::operator==(const Boy& boy)
{
	if (power() == boy.power()) {
		return true;
	}
	else {
		return false;
	}
}

std::string Boy::description(void)
{
	std::stringstream ret;
	ret << "ID:" << id << "\t姓名:" << name << "\t年龄:" << age << "\t薪资:"
		<< salary << "\t黑马系数:" << darkHorse;
	return ret.str();
}

int Boy::power() const
{
	// 薪资* 黑马系数 + (100 - 年龄) * 1000
	int value = salary * darkHorse + (100 - age) * 100;
	return value;
}

main.cpp

#include <iostream>
#include "boy.h"

int main(void) {
	Boy boy1("Rock", 38, 58000, 5);
	Boy boy2("Jack", 25, 50000, 10);
	
	if (boy1 > boy2) {
		std::cout << "选择boy1" << std::endl;
	}
	else if (boy1 == boy2) {
		std::cout << "难以选择" << std::endl;
	}
	else {
		std::cout << "选择boy2" << std::endl;
	}

	system("pause");
	return 0;
}
常见错误总结

const导致的异常BUG
小结:
const对象,只能调用对应的const方法
所以:
类的成员函数,如果已经确定不会修改任何数据成员,
那么,最好把这个成员函数,定义为const函数(在函数体的前面,参数列表的后面添加const)

错误代码,详见:《常见错误总结-错误1》

operator=的参数问题
赋值运算符的重载,应该使用这种方式:
Boy& operator=(const Boy &boy);
就是:参数要使用引用!

如果定义成:
Boy& operator=(const Boy *boy);
将会没有效果,编译器不会识别为赋值运算符的重载,
也就是:boy2 = boy1时不会调用这个函数

如果定义:
Boy& operator=(const Boy boy);
有效果,但是在调用时,会执行参数的传递:
比如:boy2 = boy1;
就会执行: boy2.operator=(boy1);
就会执行: const Boy boy = boy1;
就会执行: Boy类的赋值构造函数
有两个影响:
1)浪费性能
2)如果没有自定义的拷贝构造函数,而且这个类又有指针成员时,就会调用自动生成的拷贝构造函数,导致浅拷贝
如果析构函数中,对这个指针指向的内存做了释放,那就导致数据损坏或崩溃!

小结:
1)赋值运算符的重载,一定要使用引用参数
2)如果一个类有指针成员,而且使用了动态内存分配,那么一定要定义自己的拷贝构造函数【要使用深拷贝】,避免调用自动生成的拷贝构造函数
因为自动生成的拷贝构造函数,是浅拷贝!

  • 11
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Respect@

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值