Day05C++运算符重载



运算符重载基本概念(这是我学的最蛋疼的一天,就学了一个operator关键字)

 运算符重载(operator overloading)只是一种语法上的方便”,也就是它只是另一种函数调用的方式。

c++中,可以定义一个处理类的新运算符。这种定义很像一个普通的函数定义,只是函数的名字由关键字operator及其紧跟的运算符组成。差别仅此而已。它像任何其他函数一样也是一个函数,当编译器遇到适当的模式时,就会调用这个函数。



运算符重载碰上友元函数

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

class Person{
	friend ostream& operator<<(ostream& os, Person& person);
public:
	Person(int id,int age){
		mID = id;
		mAge = age;
	}
private:
	int mID;
	int mAge;
};

ostream& operator<<(ostream& os, Person& person){
	os << "ID:" << person.mID << " Age:" << person.mAge;
	return os;
}

int main(){

	Person person(1001, 30);
	//cout << person; //cout.operator+(person)
	cout << person << " | " << endl;

	return EXIT_SUCCESS;
}


可重载的运算符

除了赋值号(=)外,基类中被重载的操作符都将被派生类继承。





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


重载的++--运算符有点让人不知所措,因为我们总是希望能根据它们出现在所作用对象的前面还是后面来调用不同的函数。解决办法很简单,例如当编译器看到++a(前置++),它就调用operator++(a),当编译器看到a++(后置++),它就会去调用operator++(a,int).


class Complex{
	friend ostream& operator<<(ostream& os,Complex& complex){
		os << "A:" << complex.mA << " B:" << complex.mB << endl;
		return os;
	}
public:
	Complex(){
		mA = 0;
		mB = 0;
	}
	//重载前置++
	Complex& operator++(){
		mA++;
		mB++;
		return *this;
	}
	//重载后置++
	Complex operator++(int){	
		Complex temp;
		temp.mA = this->mA;
		temp.mB = this->mB;
		mA++;
		mB++;
		return temp;
	}
	//前置--
	Complex& operator--(){
		mA--;
		mB--;
		return *this;
	}
	//后置--
	Complex operator--(int){
		Complex temp;
		temp.mA = mA;
		temp.mB = mB;
		mA--;
		mB--;
		return temp;
	}
	void ShowComplex(){
		cout << "A:" << mA << " B:" << mB << endl;
	}
private:
	int mA;
	int mB;
};

void test(){
	Complex complex;
	complex++;
	cout << complex;
	++complex;
	cout << complex;

	Complex ret = complex++;
	cout << ret;
	cout << complex;

	cout << "------" << endl;
	ret--;
	--ret;
	cout << "ret:" << ret;
	complex--;
	--complex;
	cout << "complex:" << complex;
}

优先使用++--的标准形式,优先调用前置++

如果定义了++c,也要定义c++,递增操作符比较麻烦,因为他们都有前缀和后缀形式,而两种语义略有不同。重载operator++operator--时应该模仿他们对应的内置操作符。

对于++--而言,后置形式是先返回,然后对象++或者--,返回的是对象的原值。前置形式,对象先++--,返回当前对象,返回的是新对象。其标准形式为:

2016-07-12_132032

调用代码时候,要优先使用前缀形式,除非确实需要后缀形式返回的原值,前缀和后缀形式语义上是等价的,输入工作量也相当,只是效率经常会略高一些,由于前缀形式少创建了一个临时对象。


赋值(=)运算符重载


赋值符常常初学者的混淆。这是毫无疑问的,因为’=’在编程中是最基本的运算符,可以进行赋值操作,也能引起拷贝构造函数的调用。

class Person{
	friend ostream& operator<<(ostream& os,const Person& person){
		os << "ID:" << person.mID << " Age:" << person.mAge << endl;
		return os;
	}
public:
	Person(int id,int age){
		this->mID = id;
		this->mAge = age;
	}
	//重载赋值运算符
	Person& operator=(const Person& person){
		this->mID = person.mID;
		this->mAge = person.mAge;
		return *this;
	}
private:
	int mID;
	int mAge;
};

//1. =号混淆的地方
void test01(){
	Person person1(10, 20);
	Person person2 = person1; //调用拷贝构造
	//如果一个对象还没有被创建,则必须初始化,也就是调用构造函数
	//上述例子由于person2还没有初始化,所以会调用构造函数
	//由于person2是从已有的person1来创建的,所以只有一个选择
	//就是调用拷贝构造函数
	person2 = person1; //调用operator=函数
	//由于person2已经创建,不需要再调用构造函数,这时候调用的是重载的赋值运算符
}
//2. 赋值重载案例
void test02(){
	Person person1(20, 20);
	Person person2(30, 30);
	cout << "person1:" << person1;
	cout << "person2:" << person2;
	person2 = person1;
	cout << "person2:" << person2;
}
//常见错误,当准备给两个相同对象赋值时,应该首先检查一下这个对象是否对自身赋值了
//对于本例来讲,无论如何执行这些赋值运算都是无害的,但如果对类的实现进行修改,那么将会出现差异;
//3. 类中指针
class Person2{
	friend ostream& operator<<(ostream& os, const Person2& person){
		os << "Name:" << person.pName << " ID:" << person.mID << " Age:" << person.mAge << endl;
		return os;
	}
public:
	Person2(char* name,int id, int age){
		this->pName = new char[strlen(name) + 1];
		strcpy(this->pName, name);
		this->mID = id;
		this->mAge = age;
	}
#if 1
	//重载赋值运算符
	Person2& operator=(const Person2& person){

		//注意:由于当前对象已经创建完毕,那么就有可能pName指向堆内存
		//这个时候如果直接赋值,会导致内存没有及时释放
		if (this->pName != NULL){
			delete[] this->pName;
		}

		this->pName = new char[strlen(person.pName) + 1];
		strcpy(this->pName,person.pName);
		this->mID = person.mID;
		this->mAge = person.mAge;
		return *this;
	}
#endif
	//析构函数
	~Person2(){
		if (this->pName != NULL){
			delete[] this->pName;
		}
	}
private:
	char* pName;
	int mID;
	int mAge;
};

void test03(){
	Person2 person1("John",20, 20);
	Person2 person2("Edward",30, 30);
	cout << "person1:" << person1;
	cout << "person2:" << person2;
	person2 = person1;
	cout << "person2:" << person2;
}

 如果没有重载赋值运算符,编译器会自动创建默认的赋值运算符重载函数。行为类似默认拷贝构造,进行简单值拷贝。


不要重载&&||


不能重载operator&& operator|| 的原因是,无法在这两种情况下实现内置操作符的完整语义。说得更具体一些,内置版本版本特殊之处在于:内置版本的&&||首先计算左边的表达式,如果这完全能够决定结果,就无需计算右边的表达式了--而且能够保证不需要。我们都已经习惯这种方便的特性了。


我们说操作符重载其实是另一种形式的函数调用而已,对于函数调用总是在函数执行之前对所有参数进行求值。


强化训练——字符串实现








#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;

class MyString{
	//左移运算符重载
	friend ostream& operator<<(ostream& out,MyString& str);
	//右移运算符重载
	friend istream& operator>>(istream& in, MyString& str);
public:

	//有参构造函数
	MyString(const char* str){
		pString = new char[strlen(str) + 1];
		strcpy(pString, str);
		mSize = strlen(str);
	}

	//拷贝构造
	MyString(const MyString& str){
		this->pString = new char[strlen(str.pString) + 1];
		strcpy(this->pString,str.pString);
		this->mSize = str.mSize;
	}

	//重载[]操作符
	char& operator[](int index){
		return this->pString[index];
	}

	//重载+号操作符
	MyString operator+(const MyString& str){
		
		//开辟新字符串空间
		int len = this->mSize + str.mSize + 1;
		char* newStr = new char[this->mSize + str.mSize + 1];
		for (int i = 0; i < len;i++){
			newStr[i] = '\0';
		}
		strcat(newStr,this->pString);
		strcat(newStr, str.pString);

		//创建临时对象
		MyString temp(newStr);
		//释放newStr的内存
		delete[] newStr;
		return temp;
	}


	~MyString(){
		if (this->pString != NULL){
			delete[] this->pString;
			this->pString = NULL;
		}
	}
private:
	char* pString; //指向实际存放字符串的堆空间
	int mSize;//字符串长度
};

ostream& operator<<(ostream& out, MyString& str){
	out << str.pString;
	return out;
}

istream& operator>>(istream& in, MyString& str){
	if (str.pString != NULL){
		delete[] str.pString;
		str.pString = NULL;
		str.mSize = 0;
	}

	//创建临时Buf
	char buf[1024] = { 0 };
	in >> buf;

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

	return in;
}

void test(){

	MyString str1 = "hello world!";
	MyString str2 = str1;
	str2[1] = 'A';
	cout << str2[1] << endl;07 强化训练_字符串实现

	MyString str3 = str1 + str2;

	cout << str3 << endl;

	MyString str4 = "John";
	cin >> str4;

	cout << "str4:" << str4 << endl;


}


int main(){

	test();

	system("pause");
	return EXIT_SUCCESS;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值