运算符重载

+-*/++--==、!=* ->&&||...

对于内置数据类型,编译器知道如何做运算,编译器不知道如何让两个类进行运算

  1. 如果向让自定义数据类型 进行+法运算,就需要重载+运算符
  2. 在成员函数或者全局函数里 ,重写一个+法运算符的函数
  3. 函数名 operator+(){}
  4. 运算符重载也可以提供多个版本

加法运算符

类名+operator+(){};

例:

成员函数

class Person
{
public:
	Person()
	{}
	Person(int a, int b) :a(a), b(b)
	{}
	Person operator+(Person& p)
	{
		Person tmp;
		tmp.a = this->a + p.a;
		tmp.b = this->b + p.b;
		return tmp;
	}
	int a;
	int b;
};
void test()
{
	Person p1(1, 20);
	Person p2(3, 34);
	Person p3 = p2 + p1;
	cout << p3.a << "  " << p3.b << endl;
}

例:

全局函数

class Person
{
public:
	Person()
	{}
	Person(int a, int b) :a(a), b(b)
	{}

	int a;
	int b;
};
	Person operator+(Person &p1,Person& p2)
	{
		Person tmp;
		tmp.a = p1.a + p2.a;
		tmp.b = p1.b + p2.b;
		return tmp;
	}
void test()
{
	Person p1(1, 20);
	Person p2(3, 34);
	Person p3 = p2 + p1;
	cout << p3.a << "  " << p3.b << endl;
}

左移运算符重载

  • 不要随意乱用符号重载
  • 内置数据类型的运算符不可以重载
  • cou<<直接对Person自定义数据类型 进行输出
  • 写到全局函数中 ostream& operator<<(ostream& cout,Person &p){}
  • 如果重载时候想访问p的私有成员,那么全局函数要做person的友元函数

例:

class Person
{
//友元函数
	friend ostream& operator<<(ostream& cout, Person& p);
public:
	Person()
	{}
	Person(int a, int b) :a(a), b(b)
	{}
private:
	int a;
	int b;
};

//<<重载
ostream& operator<<(ostream& cout, Person& p)
{
	cout << "a=" << p.a << "b=" << p.b;
	return cout;
}
void test()
{
	Person p(23, 54);
	cout << p << endl;
}

++运算符重载

重载++运算符 operator++()前置 operator++(int)后置

前置理念:先++,后返回自身

后置理念:先保存住原值,内部++,返回临时数据

例:

class MyInteter
{
	
	friend ostream& operator<<(ostream &cout, MyInteter myInteter);
	
public:
	MyInteter()
	{
		num = 0;
	};
	//前置++重载
	MyInteter& operator++()
	{
		this->num++;
		return *this;
	};
	//后置++重载  tmp是临时数据,数据结束会销毁,所以要返回值
	MyInteter operator++(int)
	{
		MyInteter tmp=*this;
		num++;
		return tmp;
	}
private:
	int num;
};
//此处参数如果是引用,后置++返回的数据已经销毁了,会导致错误,因此要传值
ostream& operator<<(ostream& cout, MyInteter myInt)
{
	cout << myInt.num;
	return cout;
};
void test()
{
	MyInteter my;

	cout << ++my << endl;
	cout <<my++<< endl;
	cout << my << endl;
}

指针运算符重载(智能指针)

用来托管自定义类型的对象,让对象进行自动的释放。

有了智能指针,让智能指针托管这个person对象,对象的释放就不用操心了,让智能指针管理。

为了让智能指针向普通的Person*指针一样使用,就要重写->*

例:

class Person
{
public:
	Person()
	{

	}
	Person(int age)
	{
		this->age = age;
	}
	~Person()
	{
		cout << "Person析构了" << endl;
	}
	void showAge()
	{
		cout << "年龄为:" << this->age << endl;
	}
	int age;
};

class smartPointer
{
public:
	smartPointer()
	{

	}
	smartPointer(Person* p)
	{
		this->person = p;
	}
	//重载指针
	Person* operator->()
	{
		return this->person;
	}
	//重载*
	Person& operator*()
	{
		return *person;
	}
	~smartPointer()
	{
		cout << "智能指针析构了" << endl;
		if (this->person)
		{
			delete this->person;
			this->person = NULL;
		}
	}
private:
	Person* person;
};

void test()
{
	smartPointer sn(new Person(10));
	sn->showAge();
	 (*sn).showAge();
}

赋值运算符重载

系统默认给类提供的赋值运算符写法是简单值拷贝,所以要重载=运算符

如果想链式编程,就要重写=运算符

class Person
{
public:
	Person()
	{

	}
	Person(char* name)
	{//开辟新空间
		this->name =new char[strlen(name)+1];
		//拷贝数据
		strcpy(this->name, name);
	}
	~Person()
	{
		//判断是否为空,不为空释放
		if (this->name)
		{
			delete[] this->name;
			this->name = NULL;
		}
	}
	//重载=号运算符
	Person& operator=(Person &p)
	{
	//判断是否为空,不为空释放
		if (this->name)
		{
			delete[] this->name;
			this->name = NULL;
		}
		//开辟新空间
		this->name = new char[strlen(p.name) + 1];
		拷贝数据
		strcpy(this->name,p.name);
		return *this;
	}
	char* name;
};



void test()
{
	
	Person p1((char*)"小红");
	
	Person p2((char*)"小花");
	p1 = p2;
	cout << p1.name << endl;
}

[]运算符重载

返回数组索引的引用

int& operator[](int index)
pAddress是数组指针
return this->pAddress[index]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值