//MyString.cpp
#include "MyString.h"
MyString::MyString()
{
cout << "MyString()" << endl;
this->len = 0;
this->str = new char[this->len + 1];
strcpy(this->str,"");
}
//MyString::MyString(int len)
//{
// if (len <= 0)
// {
// this->len = 0;
// this->str = new char[this->len + 1];
// strcpy(this->str, "");
// }
// else
// {
// this->len = len;
// this->str = new char[this->len + 1];
// strcpy(this->str,str);
// }
//}
MyString::MyString(const char* str)
{
cout << "MyString(const char* str)" << endl;
if (str == NULL)
{
this->len = 0;
this->str = new char[this->len + 1];
strcpy(this->str, "");
}
else
{
this->len = strlen(str);
this->str = new char[this->len + 1];
strcpy(this->str, str);
}
}
MyString::MyString(const MyString& another)
{
cout << "MyString(const MyString& another)" << endl;
if (another.len <= 0)
{
this->len = 0;
this->str = new char[this->len + 1];
strcpy(this->str, "");
}
else
{
this->len = another.len;
this->str = new char[this->len + 1];
strcpy(this->str, another.str);
}
}
MyString::~MyString()
{
if (this->str != NULL)
{
cout << "~MyString()" << endl;
delete[] this->str;
this->str = NULL;
this->len = 0;
}
}
char& MyString::operator[](int index)
{
return this->str[index];
}
MyString& MyString::operator=(const MyString& another)
{
if (this == &another)
{
return *this;
}
if (this->str != NULL)
{
delete[] this->str;
this->str = NULL;
this->len = 0;
}
this->len = another.len;
this->str = new char[this->len + 1];
strcpy(this->str, another.str);
cout << "MyString & MyString::operator=(MyString & another)" << endl;
return *this;
}
#if 0
MyString& MyString::operator+(MyString& another)
{
MyString tmp;
if (this->str == NULL)
{
this->len = another.len;
this->str = new char[this->len + 1];
strcpy(this->str,another.str);
}
else
{
tmp.len = this->len;
tmp.str = new char[tmp.len + 1];
strcpy(tmp.str,this->str);
delete[] this->str;
this->str = NULL;
this->len = 0;
this->len = tmp.len + another.len;
this->str = new char[this->len + 1];
memset(this->str,0,this->len + 1);
strcpy(this->str,tmp.str);
strcat(this->str,another.str);
}
return *this;
}
#endif // 0
MyString MyString::operator+(MyString& another)
{
MyString tmp;
tmp.len = this->len + another.len;
tmp.str = new char[tmp.len + 1];
memset(tmp.str, 0, tmp.len + 1);
strcpy(tmp.str, this->str);
strcat(tmp.str, another.str);
return tmp;
}
ostream& operator<<(ostream& os, MyString& s)
{
cout << "ostream& operator<<(ostream& os, MyString& str)" << endl;
os << s.str;
return os;
}
istream& operator>>(istream& is, MyString& s)
{
cout << "istream& operator>>(istream& is, MyString& str)" << endl;
if (s.str != NULL)
{
delete[] s.str;
s.str = NULL;
s.len = 0;
}
char tmp_str[4096];
is >> tmp_str;
s.len = strlen(tmp_str);
s.str = new char[s.len + 1];
strcpy(s.str,tmp_str);
return is;
}
//MyString.h
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
using namespace std;
class MyString
{
public:
MyString();
//MyString(int len);
MyString(const char* space);
MyString(const MyString& another);
~MyString();
//重载<<操作符
friend ostream& operator<<(ostream& os, MyString& s);
//重载>>操作符
friend istream& operator>>(istream& is, MyString& s);
//重载==操作符
//重载!=操作符
//重载+操作符
MyString operator+(MyString& another);
//重载=操作符
MyString& operator=(const MyString& another);
//重载[]操作符
char& operator[](int index);
private:
int len;
char* str;
};
//main.cpp
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cstring>
#include "MyString.h"
using namespace std;
int main(void)
{
MyString str1 = "abcd";
cout << str1 << endl;
//MyString str2 = "zxcv";
//str1 = str2;
//cout << str1 << endl;
MyString str3 = "1234";
//(str3 + str1) + str1;
cout << (str3 + str1) + str1 << endl;
//cout << str3 << endl;
system("pause");
return 0;
}
为什么提示我没有匹配。教学视频这样写的都没问题