#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<string>
#include<string.h>
using namespace std;
class MyString
{
friend istream& operator >> (istream& in, MyString& str);
friend ostream& operator<<(ostream& os, MyString& str);
public:
MyString(const char* s);
MyString(const MyString& s);
MyString& operator=(const MyString& str);
MyString& operator=(const char* s);
char& operator[](int index);
MyString operator+(const MyString& str);
MyString operator+(const char* s);
const char* c_str();
bool operator==(const MyString& str);
bool operator==(const char* s);
MyString& operator+=(const MyString& str);
MyString& operator+=(const char* s);
~MyString();
private:
char* pAddr;
int mSize;
};`在这里插入代码片
#include "MyString.h"
istream& operator >> (istream& in, MyString& str)
{
cout << ">> operator\n";
char buf[256] = { 0 };
in >> buf;
if (str.pAddr){
delete[] str.pAddr;
str.pAddr = NULL;
}
str.pAddr = new char[strlen(buf) + 1];
strcpy(str.pAddr, buf);
str.mSize = strlen(buf);
return in;
}
ostream& operator<<(ostream& os, MyString& str)
{
os << str.pAddr;
return os;
}
MyString::MyString(const char* s)
{
cout << "char* construct" << endl;
this->pAddr = new char[strlen(s) + 1];
strcpy(this->pAddr, s);
this->mSize = strlen(s);
}
MyString::MyString(const MyString& s)
{
cout << "copy construct" << endl;
//指针需要手动控制拷贝过程。默认拷贝只是值拷贝,浅拷贝
this->pAddr = new char[strlen(s.pAddr) + 1];
strcpy(this->pAddr, s.pAddr);
this->mSize = s.mSize;
}
MyString& MyString::operator=(const MyString& str)
{
cout << "string operator=" << endl;
if (this->pAddr) {
delete[] this->pAddr;
this->pAddr = NULL;
}
this->pAddr = new char[strlen(str.pAddr) + 1];
strcpy(this->pAddr, str.pAddr);
this->mSize = str.mSize;
return *this;
}
MyString& MyString::operator=(const char* s)
{
cout << "char* operator=" << endl;
if (this->pAddr) {
delete[] pAddr;
pAddr = NULL;
}
pAddr = new char[strlen(s) + 1];
strcpy(pAddr, s);
mSize = strlen(s);
return *this;
}
char& MyString::operator[](int index)
{
return pAddr[index];
}
MyString MyString::operator+(const MyString& str)
{
cout << "string operator+" << endl;
int newsize = this->mSize + str.mSize + 1;
char* tmp = new char[newsize];
memset(tmp, 0, newsize);
strcat(tmp, this->pAddr);
strcat(tmp, str.pAddr);
MyString newstr(tmp);
delete[] tmp;
return newstr;
}
MyString MyString::operator+(const char* s)
{
cout << "char* operator+" << endl;
int newsize = this->mSize + strlen(s) + 1;
char* tmp = new char[newsize];
memset(tmp, 0, newsize);
strcat(tmp, this->pAddr);
strcat(tmp, s);
MyString newstr(tmp);
delete[] tmp;
return newstr;
}
const char* MyString::c_str()
{
return this->pAddr;
}
bool MyString::operator==(const MyString& str)
{
cout << "string operator==" << endl;
if (strcmp(this->pAddr, str.pAddr) == 0 && this->mSize == str.mSize)
{
return true;
}
return false;
}
bool MyString::operator==(const char* s)
{
cout << "char* operator==" << endl;
if (strcmp(this->pAddr, s) && this->mSize == strlen(s)) {
return true;
}
return false;
}
MyString& MyString::operator+=(const MyString& str)
{
cout << "string operator+=" << endl;
MyString newstr(this->pAddr);
int newsize = this->mSize + str.mSize + 1;
if (this->pAddr) {
delete[] this->pAddr;
this->pAddr = NULL;
}
this->pAddr = new char[newsize];
memset(this->pAddr, 0,newsize);
strcat(this->pAddr, newstr.pAddr);
strcat(this->pAddr, str.pAddr);
this->mSize = newsize;
return *this;
}
MyString& MyString::operator+=(const char* s)
{
cout << "char* operator+=" << endl;
MyString newstr(this->pAddr);
int newsize = this->mSize + strlen(s) + 1;
if (this->pAddr) {
delete[] this->pAddr;
this->pAddr = NULL;
}
this->pAddr = new char[newsize];
memset(this->pAddr, 0, newsize);
strcat(this->pAddr, newstr.pAddr);
strcat(this->pAddr, s);
this->mSize = newsize;
return *this;
}
MyString::~MyString()
{
cout << "destruct\n";
if (this->pAddr)
{
delete[] pAddr;
this->pAddr = NULL;
}
}
```cpp
#include "MyString.h"
void test02()
{
MyString s1 = "aaa";
MyString s2 = s1;
cout << "s1:" << s1 << endl;
cout << "s2:" << s2 << endl;
const char* s3 = "ccc";
s1 = s3;
cout << "s1:" << s1 << endl;
s1 = s2;
cout << "s1:" << s1 << endl;
s1 += "bbb";
cout << "s1:" << s1 << endl;
s1 += s3;
cout << "s1:" << s1 << endl;
cout << "s1[2]: " << s1[2] << endl;
s1[2] = '_';
cout << "s1:" << s1 << endl;
}
void test021() {
MyString s1("hello");
MyString s2(s1);
MyString s3 = "world";
if (s1 == s2) {
cout << "s1 == s2" << endl;
}
else
{
cout << "s1 != s2" << endl;
}
if (s1 == s3) {
cout << "s1 == s3" << endl;
}
else
{
cout << "s1 != s3" << endl;
}
cout << "enter s3\n";
cin >> s3;
cout << "s3:" << s3 << endl;
}
int main()
{
test021();
cout << "hello world ! ... \n";
system("pause");
return EXIT_SUCCESS;
}
MyString类的C++实现
于 2022-05-11 21:52:31 首次发布