c++核心技术06(运算符重载)

运算符重载基本概念

  运算符重载(operator overloading),就是对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型。而实际上它只是一种”语法上的方便”,也就是它只是另一种函数调用的方式。
  在c++中,可以定义一个处理类的新运算符。这种定义很像一个普通的函数定义,只是函数的名字由关键字operator及其紧跟的运算符组成。差别仅此而已。它像任何其他函数一样也是一个函数,当编译器遇到适当的模式时,就会调用这个函数。
  语法:
  定义重载的运算符就像定义函数,只是该函数的名字是operator@,这里的@代表了被重载的运算符。函数的参数中参数个数取决于两个因素:
  1. 运算符是一元(一个参数)的还是二元(两个参数);
  2. 运算符被定义为全局函数(对于一元是一个参数,对于二元是两个参数)还是成员函数(对于一元没有参数,对于二元是一个参数-此时该类的对象用作左耳参数)
  下面让我们实际放几个代码感受一下运算符重载:
  成员函数重载加号运算符代码如下:

#include <iostream>

using namespace std;

class Person
{
public:
	Person() {};
	Person(int a, int b)
	{
		m_A = a;
		m_B = b;
	}

	//利用成员函数实现加号运算符重载
	Person operator+(Person &p)
	{
		Person temp;
		temp.m_A = this->m_A + p.m_A;
		temp.m_B = this->m_B + p.m_B;
		return temp;
	}

	int m_A;
	int m_B;
};

void test01()
{
	Person p1(10, 10);
	Person p2(20, 20);

	Person p3 = p1 + p2;

	cout << "p3.m_A=" << p3.m_A << "p3.m_B=" << p3.m_B << endl;
}

int main()
{
	test01();
	return 0;
}

  利用全局函数重载加号运算符:

#include <iostream>

using namespace std;

class Person
{
public:
	Person() {};
	Person(int a, int b)
	{
		m_A = a;
		m_B = b;
	}
	int m_A;
	int m_B;
};

//利用全局函数实现加号运算符重载
Person operator+(Person& p1, Person& p2)
{
	Person temp;
	temp.m_A = p1.m_A + p2.m_A;
	temp.m_B = p1.m_B + p2.m_B;
	return temp;
}

void test01()
{
	Person p1(10, 10);
	Person p2(20, 20);

	Person p3 = p1 + p2;

	cout << "p3.m_A=" << p3.m_A << "p3.m_B=" << p3.m_B << endl;
}

int main()
{
	test01();
	return 0;
}

  所以综上我们可以看出来,对比我们可以如下图:
在这里插入图片描述

  注意:并且为了代码的可读性,尽量少用重载运算符,并且不要重载运算符改变内置数据类型表达式的运算符!


运算符重载碰上友元函数

  友元函数是一个全局函数,和我们上例写的全局函数类似,只是友元函数可以访问某个类私有数据。
  代码如下:

#include <iostream>

using namespace std;

class Person {
public:
	Person(int a, int b)
	{
		this->m_A = a;
		this->m_B = b;
	}

	/*
	* 不建议使用这种方法进行<<运算符重载,因为这种方法达不到我们想要的cout << p;
	* 所以这里要用全局函数实现<<运算符重载
	*/
	利用成员函数 做<<重载
	//void operator<<(Person& p)//p.operator<<(cout)  p<<cout
	//{

	//}
	friend ostream& operator << (ostream& cout, Person& p1);
private:
	int m_A;
	int m_B;
};

ostream& operator<<(ostream &cout,Person &p1)
{
	cout << "m_A = " << p1.m_A << "m_B" << p1.m_B;
	return cout;
}

void test01()
{
	Person p1(10, 10);
	cout << p1 << endl;
}

int main()
{
	test01();
	return 0;
}

运算符重载的规则

  在c++中大部分运算符都可以重载,这里我们只要稍微记一下不能重载的运算符就可,以下运算符是不能重载的:

成员访问.
域运算::
内存长度运算sizeof
三目运算?::
预处理#

  运算符重载的注意事项:
  1. 为了防止对标准类型进行运算符重载,c++规定重载运算符的操作对象至少有一个不是标准类型,而是用户自定义的类型,比如不能重载1+2,但是可以重载 cow + 2 和 2 + cow //cow是自定义的对象
  2、不能改变原运算符的语法规则,比如不能把双目运算符重载为单目运算
  3、不能改变运算符的优先级
  4、不能创建新的运算符,比如operator**就是非法的,operator*是可以的
  5、不能对以下这四种运算符,使用友元函数进行重载 = 赋值运算符,()函数调用运算符,[]下标运算符,->通过指针访问类的成员
  6、如果运算符的第一个操作数要求使用隐式类型转换,则必须使用友元函数(成员函数方式的第一个参数是this指针)


自增自减(++/–)运算符重载

  这里就举前置++和后置++的例子,自减只要将++改为–就行了,代码如下:

#include <iostream>

using namespace std;

class MyInter
{
public:
	MyInter()
	{
		num = 0;
	}
	friend ostream& operator<<(ostream& cout, MyInter& p1);
	//前置++ 重载
	MyInter& operator++()
	{
		this-> num++;
		return *this;
	}

	//后置++重载
	MyInter operator++(int)//这里int是个占位符,用来区分前置++和后置++的
	{
		//先记录初始状态
		MyInter temp = *this;

		this->num++;

		return temp;
	}
private:
	int num;
};



ostream& operator<<(ostream& cout ,MyInter &myInt)
{
	cout << myInt.num;
	return cout;
}

void test01()
{
	MyInter myInt;
	++myInt;
	cout << ++myInt << endl;
}

void test02()
{
	MyInter myInt;
	myInt++;
	cout << myInt << endl;
}

int main()
{
	test01();
	//因为返回的值不是本身了,所以后置++返回的是值
	/*int b = 0;
	cout << (b++)++ << endl;*/
	test02();
	return 0;
}

  前置++和后置++区别总结:
  1. 为了区分前置++和后置++,需要在参数里加一个int做占位符
  2. 由于前置++需要做链式操作,所以返回引用,但是后置++不能做链式操作,所以返回的是一个值


指针运算符(*、->)重载

&emsp: 代码如下:

#include <iostream>

using namespace std;

class Person
{
public:
	Person(int age)
	{
		cout << "Person的有参构造函数" << endl;
		this->age = age;
	}
	void showAge()
	{
		cout << "年龄为:" << this->age << endl;
	}
	~Person()
	{
		cout << "Person的析构调用" << endl;
	}
	int age;
};

//设计smartPoint智能类,内部维护Person*,在析构时释放堆区new出来的person对象
class SmartPoint
{
public:
	SmartPoint(Person* person)
	{
		this->person = person;
	}

	//重载->运算符
	Person* operator->()
	{
		return this->person;
	}

	Person& operator*()
	{
		return *person;
	}

	~SmartPoint()
	{
		if (this->person)
		{
			delete this->person;
			this->person = NULL;
		}
	}
private:
	Person* person;
};

void test01()
{
	Person p(100); 将数据开到栈上
	//Person *p = new Person(18);
	//(*p).showAge();
	//p->showAge();
	delete p;

	//利用智能指针 管理new出来的person的释放操作
	SmartPoint sp(new Person(18));

	sp->showAge();     //sp->->showAge(); 编译器简化成一个箭头

	(*sp).showAge();  
}


int main()
{
	test01();
	return 0;
}

赋值(=)运算符重载

  代码如下:

#pragma warning(disable:4996)
#include <iostream>
#include <string.h>
#include <string>


using namespace std;



//编译器默认给一个类至少添加3个参数 默认构造 析构 拷贝构造(值拷贝)operator=(值拷贝)
class Person
{
public:
	Person(const char* name, int age)
	{
		this->name = new char[strlen(name) + 1];
		strcpy(this->name, name);
		this->age = age;
	}

	//重载=
	Person& operator=(const Person &p)
	{
		//先判断原来堆区是否有内容,如果有释放
		if (this->name != NULL)
		{
			delete[] this->name;
			this->name = NULL;
		}

		this->name = new char[strlen(p.name) + 1];
		strcpy(this->name, p.name);
		this->age = p.age;
		return *this;
	}

	//拷贝构造函数
	Person(const Person& p)
	{
		this->name = new char[strlen(p.name) + 1];
		strcpy(this->name, p.name);
		this->age = age;
	}

	~Person()
	{
		if (this->name != NULL)
		{
			delete[] this->name;
			this->name = NULL;
		}
	}
	char* name ;
	int age;
};

void test01()
{
	Person p1("Tom",10);

	Person p2("Jerry",19);
	p2 = p1;

	Person p3(" ",0);
	p3 = p2 = p1; //相当于返回void,让p3等于一个void就出错了

	Person p4 = p3;//浅拷贝,要重写拷贝构造函数

	cout << " p1.name = " << p1.name << " p1.age = " << p1.age << endl;
	cout << " p2.name = " << p2.name << " p2.age = " << p2.age << endl;
	cout << " p3.name = " << p3.name << " p3.age = " << p3.age << endl;
}

int main()
{
	test01();
	/*int a = 10;
	int b = 20;
	int c;
	c = a = b; 
	cout << "a = " << a << "b = " << b << "c = " << c << endl;*/
	return 0;
}

下标运算符重载

  C++规定,下标运算符[]必须以成员函数的形式进行重载,该重载函数在类中的声明如下:
  返回值类型 & operator
  或者:
  const 返回值类型 & operator const
  使用第一种声明方式,[]不仅可以访问元素,还可以修改元素
  使用第二种声明方式,[]只能访问而不能修改元素
 &实际开发中我们应该同时提供以上两种形式,这样做是为了适应const对象,因为通过const对象只能调用const成员函数,如果不提供第二种形式,那么将无法访问const对象的任何元素

#include <iostream>
using namespace std;
class Array
{
public:
	Array(int s)
	{
		size = s;
		array = (int*)malloc(sizeof(int) * size);
	}
	int& operator[](int Index)
	{
		return array[Index];
	}
	bool operator==(const Array& a)
	{
		return (this->array == a.array && this->size == a.size);
	}
	void operator=(const Array& a)
	{
		if (*this == a)
		{
			return;
		}//自己赋值给自己
		this->size = a.size;
		free(array);
		this->array = (int*)malloc(sizeof(int) * this->size);
		for (int i = 0; i < this->size; i++)
		{
			this->array[i] = a.array[i];
		}
	}
	~Array()
	{
		if (array != NULL)
		{
			free(array);
			array = NULL;
		}
	}
private:
	int size;
	int* array;

};
int main(int argc, const char* argv[])
{
	Array a1(10);
	a1[0] = 100;
	for (int i = 0; i < 10; i++)
	{
		a1[i] = i + 1;
	}
	for (int i = 0; i < 10; i++)
	{
		cout << a1[i] << " ";
	}
	cout << endl;
	Array a2(5);
	a2 = a1;
	//a1 = a1;
	for (int i = 0; i < 10; i++)
	{
		cout << a1[i] << " ";
	}
	cout << endl;

	return 0;
}

等于和不等于(==、!=)运算符重载

&emsp: 代码如下:

#include <iostream>
#include <string>
#include <cstring>

using namespace std;


class Person
{
public:
	Person(string name, int age)
	{
		this->name = name;
		this->age = age;
	}

	bool operator==(Person& p)
	{
		if (this->name == p.name && this->age == p.age)
		{
			return true;
		}
		return false;
	}

	bool operator != (Person& p)
	{
		if (this->name != p.name || this->age != p.age)
		{
			return true;
		}
		return false;
	}

	string name;
	int age;
};

void test01()
{
	/*int a = 10;
	int b = 20;
	if (a == b)
	{
		cout << "a == b" << endl;
	}
	else
	{
		cout << " a != b" << endl;
	}*/
	Person p1("Tom", 18);

	Person p2("Tom", 18);

	if (p1 == p2)
	{
		cout << "p1 == p2" << endl;
	}
	else
	{
		cout << "p1 != p2" << endl;
	}

	if (p1 != p2)
	{
		cout << " a != b" << endl;
	}
	else
	{
		cout << "p1 == p2" << endl;
	}

}

int main()
{
	test01();
	return 0;
}

函数调用符号()重载

  代码如下:

#include <iostream>
#include <string>

using namespace std;

class MyPrint
{
public:
	void operator()(string text)
	{
		cout << text<< endl;
	}
};

void MyPrint2(string str)
{
	cout << str << endl;
}

void test01()
{
	MyPrint myPrint;
	myPrint("hello world"); //仿函数 本质是一个对象 函数对象
	
	MyPrint2("hello world"); //普通函数
}


class MyAdd
{
public:
	int operator()(int a,int b)
	{
		return a + b;
	}
};


void test02()
{
	MyAdd myAdd;
	cout << myAdd(1, 1) << endl;

	cout << MyAdd()(1, 1) << endl;//匿名函数对象:当前执行完立即释放
}

int main()
{
	//test01();
	test02();
	return 0;
}

不要重载&&、||

  不能重载operator&& 和 operator|| 的原因是,无法在这两种情况下实现内置操作符的完整语义。&&和||有个说得更具体一些,内置版本版本特殊之处在于:内置版本的&&和||首先计算左边的表达式,如果这完全能够决定结果,就无需计算右边的表达式了–而且能够保证不需要。我们都已经习惯这种方便的特性了。
我们说操作符重载其实是另一种形式的函数调用而已,对于函数调用总是在函数执行之前对所有参数进行求值。

符号重载总结

  函数重载总结:
  1、=,[],(),->,操作符只能通过成员函数进行重载
  2、<< 和 >>只能通过全局函数配合友元函数进行重载
  3、不要重载&&和||操作符,因为无法实现短路原则
  常规建议:

运算符建议使用
所有的一元运算符成员
= () [] ->必须是成员
+= .= /= *= ^= &=成员
其他二元运算符非成员

字符串类封装

  MyString.h:

#pragma once
#pragma warning(disable:4996)
#include <iostream>
#include <cstring>

using namespace std;

class MyString
{
public:
	//有参构造
	MyString(const char* str);
	//拷贝构造
	MyString(const MyString& str);
	//析构函数
	~MyString();
	//左移运算符友元
	friend ostream& operator<<(ostream& cout, MyString& str);
	//右移运算符
	friend istream& operator>>(istream& cin, MyString& str);

	//重载=运算符 a=b=c

	MyString& operator=(const char* str);
	MyString& operator=(const MyString& str);

	//重载[]运算符
	char& operator[](int index);

	//重载+运算符
	MyString operator+(const char* str);
	MyString operator+(const MyString& str);

	//重载==运算符
	bool operator==(const char* str);
	bool operator==(const MyString& str);

private:
	char* pString;//维护在堆区开辟的字符数组

	int m_Size; //字符串长度,不统计\0
};


  MyString.cpp:

#include "MyString.h"

//左移运算符
ostream& operator<<(ostream& cout, MyString& str)
{
	cout << str.pString;
	return cout;
}


//右移运算符
istream& operator>>(istream& cin, MyString& str)
{
	//先清空原来的堆区数据
	if (str.pString)
	{
		delete[] str.pString;
		str.pString = NULL;
	}

	char buf[1024];//开辟临时数据 记录用户输入内容
	cin >> buf;

	str.pString = new char[strlen(buf) + 1];
	strcpy(str.pString, buf);
	str.m_Size = strlen(buf);

	return cin;
}

MyString::MyString(const char* str)
{
	cout << "MyString有参构造函数调用" << endl;
	this->pString = new char[strlen(str) + 1];
	strcpy(this->pString, str);
	this->m_Size = strlen(str);
}

MyString::MyString(const MyString& str)
{
	cout << "拷贝构造函数调用" << endl;
	this->pString = new char[strlen(str.pString)+1];
	strcpy(this->pString, str.pString);
	this->m_Size = str.m_Size;
}

MyString::~MyString()
{
	if (this->pString != NULL)
	{
		cout << "析构调用" << endl;
		delete[] this->pString;
		this->pString = NULL;
	}
}

MyString& MyString::operator=(const char * str)
{
	//先判断原来堆区释放有内容,如果有先释放
	if (this->pString != NULL)
	{
		delete[]this->pString;
		this->pString = NULL;
	}

	this->pString = new char[strlen(str) + 1];
	strcpy(this->pString, str);
	this->m_Size = strlen(str);
	return *this;
}


MyString& MyString::operator=(const MyString& str)
{
	//先判断原来堆区释放有内容,如果有先释放
	if (this->pString != NULL)
	{
		delete[]this->pString;
		this->pString = NULL;
	}

	this->pString = new char[strlen(str.pString) + 1];
	strcpy(this->pString, str.pString);
	this->m_Size = strlen(str.pString);
	return *this;
}

char& MyString::operator[](int index)
{
	return this->pString[index];
}

MyString MyString::operator+(const char* str)
{
	//本身abc 传入def
	//计算开辟内存大小
	int newSize = this->m_Size + strlen(str) + 1;

	char* temp = new char[newSize];
	memset(temp,0,newSize);

	strcat(temp,this->pString);
	strcat(temp, str);

	MyString newString = temp;

	delete[] temp;
	return newString;
}

MyString MyString::operator+(const MyString& str)
{
	int newSize = this->m_Size + strlen(str.pString) + 1;

	char* temp = new char[newSize];
	memset(temp, 0, newSize);

	strcat(temp, this->pString);
	strcat(temp, str.pString);

	MyString newString = temp;

	delete[] temp;
	return newString;
}

bool MyString::operator==(const char* str)
{
	if (strcmp(this->pString, str) == 0)
	{
		return true;
	}
	return false;
}

bool MyString::operator==(const MyString& str)
{
	if (strcmp(this->pString, str.pString) == 0)
	{
		return true;
	}
	return false;
}

  main.cpp:

#include "MyString.h"


void test1()
{
	MyString str("abc");
	MyString str2(str);

	cout << str << endl;

	cout << "请重新给str赋值" << endl;

	cin >> str;

	cout << "str新的值为:" << str << endl;

	MyString str3 = str;

	cout << "str3=" << str3 << endl;
}

void test02()
{
	MyString str = "abcd";

	MyString str2 = "aaa ";

	str2 = str;
	
	cout << "str2 = " << str2 << endl;

	cout << "str2[0] = " << str2[0] << endl;

	str2[0] = 'z';

	cout << "str2[0]改为z后输出:" << str2 << endl;

	MyString str3 = "abc";
	MyString str4 = "def";
	MyString str5 = str3 + str4;

	cout << "str5=" << str5 << endl;

	if (str5 == str4)
	{
		cout << "str5 == str4" << endl;
	}
	else
	{
		cout << "str5 != str4" << endl;
	}
}

int main()
{
	//test1();
	test02();
	
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值