其中还用到了右移运算符的重载
myString.h
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
class myString {
//左移运算符 友元
friend ostream& operator<<(ostream& cout, myString& str);
//右移运算符 友元
friend istream& operator>>(istream& cout, myString& str);
public:
myString(const char* str);
myString(const myString& str);
//重载=运算符
myString& operator=(const char *str);
myString& operator=(const myString& str);
//重载[]运算符
char& operator[](int index);
//重载+运算符
myString operator+(const myString& str);
//重载==运算符
bool operator==(const myString& str);
~myString();
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& cout, myString& str)
{
//先清除堆区原来的内容
if (str.pString != NULL)
{
delete[] str.pString;
str.pString = NULL;
}
char buf[1024];
cout >> buf;
str.pString = new char[strlen(buf) + 1];
strcpy(str.pString,buf);
str.m_Size = strlen(buf);
return cin;
}
//重载等号运算符
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 = str.m_Size;
return *this;
}
//重载[]运算符
char& myString::operator[](int index)
{
return this->pString[index];
}
//重载+运算符
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 newStr = temp;
delete[] temp;
return newStr;
}
//重载==运算符
bool myString::operator==(const myString& str)
{
if (strcmp(this->pString,str.pString) == 0)
{
return true;
}
else
{
return false;
}
}
myString::myString(const char* str)
{
cout << "有参构造调用" << 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()
{
cout << "析构函数调用" << endl;
if (this->pString != NULL)
{
delete[] this->pString;
this->pString = NULL;
}
}
main.cpp
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
#include "myString.h"
void test01()
{
myString mystring("abc");
myString mystring01(mystring);
cout << mystring << endl;
cout << mystring01 << endl;
cout << "请重新给mystring.pString赋值" << endl;
cin >> mystring;
cout << mystring << endl;
}
void test02()
{
myString str = "ajc";
myString str2 = "cfy";
str2 = str;
cout << "更改前:str2[0] = " << str2[0] << endl;
str2[0] = 'z';
cout << "更改后:str2[0] = " << str2[0] << endl;
myString str3 = "qwe";
myString str4 = "asd";
myString str5 = str3 + str4;
cout << "str5 = " << str5 << endl;
myString str6 = str3 + "asd";
cout << "str6 = " << str6 << endl;
if (str5 == str6)
{
cout << "str5 == str6" << endl;
}
else
{
cout << "str5 != str6" << endl;
}
}
int main()
{
//test01();
test02();
//int a;
//cin >> a;
//cout << "a = " << a << endl;
system("pause");
return EXIT_SUCCESS;
}