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
class Pork
{
public:
Pork(int weight);
std::string description(void);
private:
int weight = 0;
};
Pork.cpp
#include "Pork.h"
#include
Pork::Pork(int weight)
{
this->weight = weight;
}
std::string Pork::description(void)
{
std::stringstream ret;
ret << weight << "斤猪肉";
return ret.str();
}
main.cpp
#include
#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指针)
注意:
同一个运算符重载, 不能同时使用两种方式来重载,会导致编译器不知道选择哪一个(二义性)
运算符重载的禁区和规则
2.不能改变原运算符的语法规则, 比如不能把双目运算符重载为单目运算
3.不能改变原运算符的优先级
4.不能创建新的运算符,比如 operator**就是非法的, operator*是可以的
5.不能对以下这四种运算符,使用友元函数进行重载
= 赋值运算符,()函数调用运算符,[ ]下标运算符,->通过指针访问类成员
注意:
注意赋值运算符重载的返回类型 和参数类型。
返回引用类型,便于连续赋值
参数使用应用类型, 可以省去一次拷贝
参数使用const, 便于保护实参不被破坏。
Boy.h
#pragma once
#include
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
#include
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
#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)如果一个类有指针成员,而且使用了动态内存分配,那么一定要定义自己的拷贝构造函数【要使用深拷贝】,避免调用自动生成的拷贝构造函数
因为自动生成的拷贝构造函数,是浅拷贝!