C++:运算符重载:以String类为例

运算符重载

        运算符重载即定义一个重载运算符的函数,使指定的运算符不仅能实现原有的功能,而且能实现在函数中指定的新的功能。
        运算符重载实质上是函数的重载
重载运算符的函数一般格式如下:
       函数类型 operator 运算符名称(形参表)
       {对运算符的重载处理}
operator是关键字,是专门用于定义重载运算符的函数的,运算符名就是C++已有的运算符。

运算符重载的规则:
(1)C++不允许用户自己定义新的运算符,只能对已有的C++运算符进行重载。
(2)C++允许重载的运算符
            不能重载重载的运算符只有5个:
           ① .   成员访问运算符
           ② *   成员指针访问运算符
           ③ ::   域运算符
           ④sizeof 长度运算符
           ⑤ ?:   条件运算符

(3)重载不能改变运算符运算对象(即操作数)的个数
(4)重载不能改变运算符的优先级别
(5)重载不能改变运算符的结核性
(6)重载不能改变默认的参数
(7)重载的运算符必须和用户自定义类型的对象一起使用,其参数至少有一个是类对象(或类对象的引用)
(8)用于类对象的运算符一般必须重载,但有两个例外,“=”和“&”不必用户重载

     运算符重载的函数有两种处理方式:
(1)把运算符重载的函数作为类的成员函数
      如果运算符重载函数作为成员函数,它可以通过this指针自由地访问本类的数据成员,因此可以少写一个函数的参数。
(2)运算符重载的函数不是类的成员函数(可以是一个普通函数),在类中把它声明为友元函数
      将双目运算符重载为友元函数时,由于友元函数不是该类的成员函数,因此在函数的形参列表中必须有两个参数,不能省略。
      究竟把运算符重载作为类的成员函数好,还是友元函数好?我们一般参照以下参考:
;(1)C++规定,赋值运算符“=”、下标运算符“[]”、函数调用运算符“()”、成员运算符"->“必须作为成员函数重载。
(2)流插入“<<”和流提取运算符”>>"、类型转换运算符函数不能定义为类的成员函数,只能作为友元函数。
(3)一般将单目运算符和复合运算符( += , -= , /= , *= , &= , != , ^= , %= , >>= , <<= )重载为成员函数 。
(4)一般将双目运算符重载为友元函数。

String类为例:

下面我们以String类为例实现几个运算符的重载:

#include<iostream>
#include<string>
using namespace std;
/*
   运算符重载
*/
#pragma warning(disable:4996);//屏蔽该错误

class String
{
	private:
		char *pstr;
		friend const String operator + (char*,const String&);
		friend std::ostream& operator<<(std::ostream&,const String&);

	public:
		String(char *str)//构造函数
		{
			pstr = new char[strlen(str) + 1]();
			strcpy_s(pstr,strlen(str) + 1, str);
		}

		String(const String& rhs)//拷贝构造函数
		{
			pstr = new char[strlen(rhs.pstr) + 1]();
			strcpy_s(pstr,strlen(rhs.pstr) + 1, rhs.pstr);
		}

		String& operator=(const String&rhs)//赋值运算符的重载函数
		{
			if (this != &rhs)
			{
				delete[] pstr;
				pstr = new char[strlen(rhs.pstr) + 1]();
				strcpy_s(pstr,strlen(rhs.pstr) + 1, rhs.pstr);
			}
			return *this;
		}

		const String operator+(const String& rhs)//对象与对象函数的重载
		{
			int len = strlen(pstr) + strlen(rhs.pstr) + 1;
			char* pnewspace = new char[len]();
			strcpy_s(pnewspace,strlen(pstr) + 1, pstr);
			strcat(pnewspace, rhs.pstr);
			String tmp(pnewspace);
			delete[] pnewspace;
			return tmp;
		}

		const String operator+(char* rhs)//对象与字符串函数的重载
		{
			int len = strlen(pstr) + strlen(rhs) + 1;
			char* pnewspace = new char[len]();
			strcpy_s(pnewspace,strlen(pstr) + 1, pstr);
			strcat(pnewspace, rhs);
			String tmp(pnewspace);
			delete[] pnewspace;
			return tmp;
		}

		~String()//析构函数
		{
			delete[] pstr;
			pstr = NULL;
		}


		bool operator!=(const String& rhs)// != 运算符的重载
		{
			return strcmp(pstr, rhs.pstr) != 0;
		}

		bool operator<(const String& rhs)// < 运算符的重载
		{
			return strcmp(pstr, rhs.pstr) < 0;
		}

		char& operator[](int index) //[]运算符的重载
		{
			return pstr[index];
		}
};

const String operator+(char* lhs, const String& rhs)//字符串与对象函数的重载
{
	int len = strlen(lhs) + strlen(rhs.pstr) + 1;
	char* pnewspace = new char[len]();
	strcpy_s(pnewspace,strlen(lhs) + 1, lhs);
	strcat(pnewspace, rhs.pstr);
	String tmp(pnewspace);
	delete[] pnewspace;
	return tmp;
}

std::ostream& operator<<(std::ostream& out,const String& rhs)// <<流运算符的重载
{
	out<<rhs.pstr;
	return out;
}

int main()
{
	String str1("hello");
	String str2 = "world";

	String str3 = str1 + str2;
	str3 = str1 + "hi";
	str3 = "hi" + str2;

	if (str1 < str2)
	{
		std::cout<<str1<<std::endl;
	}

	if (str1 != str2)
	{
		std::cout<<str2<<std::endl;
	}
	char a = str1[0];
	std::cout<<a<<std::endl;

	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值