C++运算符重载、自己实现的String类

本文介绍了C++中的运算符重载原理,包括流重载、特殊运算符如++、--的重载,以及自定义字符串类的实现,包括构造函数、赋值运算符、比较运算符和流插入/提取操作符的重载。示例代码展示了如何在类中使用成员函数和友元函数进行运算符重载,以及如何实现隐式类型转换。此外,还提供了一个简单的自定义MyString类,包含了字符串的基本操作和流操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

运算符重载
重载本质上是函数调用,并且一个运算符在同一个类中只能被重载一次

  1. = () -> [] 只能采用成员函数的方式重载
  2. . .* ?: ::这些不能被重载
  3. 习惯: 单目运算符采用类成员函数方式重载,双目运算符采用友元函数方式实现

有如下分类:

  1. 流重载: 只能采用友元函数方式重载 流重载函数返回值必须是引用
    输入流重载: 例子: string name; cin>>name; 分支cin istream类的对象
    输出流重载:例子: string name; cout<<name; 分支cout ostream类的对象

  2. 特殊运算符重载
    ++ --重载 (要区分前置和后置)
    对于前置和后置的区分
    opeartor++(int) //这里是后置++ 这里函数int是一个占位符 用于区分前置或者后置
    opeartor++() //这里是前置++

    文本重载(重载后缀)
    参数一定是unsigned long long
    一般做下划线的系列和库中做一个区分

unsigned long long operator""_h(unsigned long long num)
{
	return num * 60 * 60;
}

void printSec(int num)
{
	cout << num << endl;
}

调用:
printSec(1_h);

3.隐式转换

重载()

//这里重载了() 函数调用特殊运算符
class Object {
public:
	bool operator()(int i, int j)
	{
		cout << "operator()被调用" << endl;
		return i > j;
	}
};

调用:
Object object;
object(1, 2);  //第一种方式用对象
Object()(1, 2); //第二种方式用类名

自己实现的string类

#include <iostream>
#include <string>

using namespace std;

class MyString {
public:
	MyString()
	{
		cout << "MyString无参构造函数" << endl;
	}
	MyString(const char* str)
	{
		cout << "调用构造函数" << endl;
		cout << "strlen(str)=" << strlen(str) << endl;
		string = new char[strlen(str) + 1];
		strcpy_s(string, strlen(str) + 1, str);
		this->m_size = strlen(str);
	}

	MyString(const MyString& myStr)
	{
		cout << "调用拷贝构造函数" << endl;
		if (this->string != NULL)
		{
			delete[] this->string;
			this->string = NULL;
		}

		string = new char[strlen(myStr.string) + 1];
		strcpy_s(this->string, strlen(myStr.string) + 1, myStr.string);
		this->m_size = strlen(myStr.string);
	}
	MyString& operator=(const MyString& myStr)
	{
		if (this->string != NULL)
		{
			delete[] this->string;
			this->string = NULL;
		}

		string = new char[strlen(myStr.string) + 1];
		strcpy_s(this->string, strlen(myStr.string) + 1, myStr.string);
		this->m_size = strlen(myStr.string);
		return *this;
	}
	MyString operator+(MyString& myStr)
	{
		int newSize = this->m_size + myStr.m_size + 1;
		char *temp= new char[newSize];
		memset(temp, 0, newSize);
		strcat(temp, this->string);
		strcat(temp, myStr.string);
		MyString tempString(temp);
		delete[] temp;
		return tempString;
	}

	MyString operator+(const char *str)
	{
		int newSize = this->m_size + strlen(str) + 1;
		char* temp = new char[newSize];
		memset(temp, 0, newSize);
		strcat(temp, this->string);
		strcat(temp, str);
		MyString tempString(temp);
		if (temp)
		{
			delete[] temp;
		}
		return tempString;
	}
	bool operator==(const char*str)
	{
		if (!strcmp(this->string, str) && this->m_size == strlen(str))
		{
			return true;
		}
		else
			return false;
	}

	bool operator==(MyString &myStr)
	{
		if (!strcmp(this->string, myStr.string) && this->m_size == strlen(myStr.string))
		{
			return true;
		}
		else
			return false;
	}
	~MyString()
	{
		//cout << "调用析构函数" << endl;
		if (this->string != NULL)
		{
			delete[] this->string;
			this->string = NULL;
		}
	}
	void print()
	{
		cout << this->string << endl;
	}
	friend ostream& operator<<(ostream& cout, MyString& str);
	friend istream& operator>>(istream& cin, MyString& str);
protected:
	char* string;
	int m_size;  //字符长度
};

ostream& operator<<(ostream& cout, MyString& str)
{
	if (!str.string)
	{
		cout << "";
	}
	else {
		cout << str.string;
	}
	return cout;
}

istream& operator>>(istream& cin, MyString& str)
{
	if (str.string != NULL)
	{
		delete[] str.string;
		str.string = NULL;
	}
	char temp[10];
	cin >> temp;
	str.string = new char[strlen(temp) + 1];
	strcpy_s(str.string, strlen(temp) + 1, temp);
	str.m_size = strlen(temp);
	return cin;
}

int main()
{
	char str[] = "hello";
	MyString myStr("hello");
	//myStr.print();

	MyString myStr2(myStr);
	//myStr2.print();
	cout << myStr<<"\t" << myStr2<<endl;

	cout << "mystr3:" << endl;
	MyString myStr3;
	cout << myStr3 << endl;

	cout << "输入:" << endl;
	//cin >> myStr3;
	myStr3 = myStr2;
	cout << myStr3 << endl;

	cout << "使用+:" << endl;
	MyString str1("str1+");
	MyString str2("str2");
	cout << str1 << endl;
	cout << str2 << endl;
	MyString str3 = str1 + str2;
	cout << str3 << endl;

	if (str3 == str2)
	{
		cout << "str3和str2相等" << endl;
	}else
		cout << "str3和str2不相等" << endl;
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值