html5 个人网站模板,网站建设策划书的撰写,葫芦岛建设信息网站,手机wap网站制作总结#xff1a;
1、等号操作符重载注意事项#xff1a; #xff08;1#xff09;防止自身赋值 #xff08;2#xff09;先将自身的额外开辟的空间回收掉 #xff08;3#xff09;执行深拷贝 2、注意函数的返回引用或者元素#xff1a;如果需要连续使用 …总结
1、等号操作符重载注意事项 1防止自身赋值 2先将自身的额外开辟的空间回收掉 3执行深拷贝 2、注意函数的返回引用或者元素如果需要连续使用 返回元素 3、如果一个函数传递const修饰的对象那么此对象调用的成员函数也应为const类型 4、重载小括号 称这种对象为仿函数 5、字符串重载时防止初始化的字符串长度为空
MyString.h:
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#includeiostream
using namespace std;
class MyString
{
public:MyString();MyString(int len); //创建一个长度为len的string对象MyString(const char*str);MyString(const MyString another);~MyString();//重载操作符[] //char* operator[](int index);char operator[](int index); //mystring.operator[](index)//重载操作符 friend ostream operator(ostream os, MyString mystring);//重载操作符friend istream operator(istream is, MyString mystring); //重载操作符bool operator(const MyString mytring2) ;//重载操作符!friend bool operator!(MyString mystring1, MyString mytring2);//重载操作符//MyString operator(const MyString another);MyString operator(const MyString another);//重载操作符 可以连加 所以返回匿名对象 不加引用//如果加引用 第一次加法后返回一个乱码值//friend MyString operator(const MyString another1, const MyString another2);MyString operator(MyString another);private:int len;char *str;
};
MyString.cpp:
#include MyString.hMyString::MyString()
{this-len 0;this-str NULL; //表示零值或空值//this-str ; //表示空字符串 里面有\0
}MyString::MyString(int len)
{this-len len;this-str new char[len];
}MyString::MyString(const char * str)
{/*meint len strlen(str);this-len len 1;this-str new char[len 1];for (int i 0; i len; i){this-str[i] str[i];}*/if (str NULL) {this-len 0;this-str new char[01]; strcpy(this-str, );}else {int len strlen(str);this-len len;this-str new char[len 1];strcpy(this-str, str);}
}MyString::MyString(const MyString another)
{//拷贝构造函数不需要回收垃圾 初始化时被调用this-len another.len;this-str new char[this-len 1];strcpy(this-str, another.str);}MyString::~MyString()
{if (this-str ! NULL) {cout this-str 执行了析构函数 endl;delete [] this-str;this-str NULL;this-len 0;}
}char MyString::operator[](int index)
{// TODO: 在此处插入 return 语句return this-str[index];
}bool MyString::operator(const MyString mystring2)
{if (this-len ! mystring2.len) {return false;}for (int i 0; i this-len; i){if (this-str[i] ! mystring2.str[i]) {return false;}}return true;
}MyString MyString::operator(const MyString another)
{if (this another) {return *this;}// TODO: 在此处插入 return 语句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);return *this;}ostream operator(ostream os, MyString mystring) {cout len:;os mystring.len;cout str:;os mystring.str ;return os;
}istream operator(istream is, MyString mystring)
{// TODO: 在此处插入 return 语句//先释放mystring内存//char *strNULL;cinstr; 程序崩溃//1、将mystring之前的字符串释放掉if (mystring.str ! NULL) {delete mystring.str;mystring.str NULL;mystring.len 0;}//2、通过cin添加新的字符串char temp_str[4096] { 0 };cin temp_str;int len strlen(temp_str);mystring.str new char[len 1];strcpy(mystring.str, temp_str);mystring.len len;return is;
}MyString MyString::operator(MyString another)
{// TODO: 在此处插入 return 语句MyString temp;int len this-len another.len;temp.len len;temp.str new char[len 1];memset(temp.str, 0, len 1); //清空 strcat需要memset strcpy不需要strcat(temp.str, this-str);strcat(temp.str, another.str);//temp.str this-str another.str;return temp;
}
bool operator!(MyString mystring1,MyString mytring2) {return !(mystring1 mytring2);
}
main.cpp:
#define _CRT_SECURE_NO_WARNINGS
#includeiostream
#includestring
#includeMyString.h
using namespace std;void test01() {string s1(123);//equal tostring s2 123;string s3(s1); //重载拷贝构造string s4; s4 s1; //重载s4 s1 s2; //重载s2[1] x; //重载[]cout s2 endl; //重载
}
/*
1x3
*/void test02() {MyString s1;MyString s2(123);//MyString s3(NULL);MyString s4(s2);//cout s1 s1 endl; 截断后面输出cout s2 s2 endl;cout s4 s4 endl;s2[1] x;cout s2 s2 endl;cout s4 s4 endl;MyString s5;s5 s2;cout s5 s5 endl;
}
/*
s2len:3str:123
s4len:3str:123
s2len:3str:1x3
s4len:3str:123
s5len:3str:1x3
1x3执行了析构函数
123执行了析构函数
1x3执行了析构函数
*/
void test03() {MyString s1(abc);cin s1;cout s1 endl;
}
/*
ddd
len:3str:ddd
ddd执行了析构函数
*/
void test04() {
#if 0string s1(123);string s2(456);cout (s1 s2)s2 endl;cout s1 endl;cout s2 endl;
#endifMyString s1(123);MyString s2(456);cout (s1 s2) endl;cout s1 endl;cout s2 endl;
}
/*
123456执行了析构函数
len:6str:123456
123456执行了析构函数
len:3str:123
len:3str:456
456执行了析构函数
123执行了析构函数
*/
void test05() {
#if 0string s1(123);string s2(456);if (s1 s2) {cout 等于 endl;}elsecout 不等于 endl;
#endif //等价于MyString s1(123);MyString s2(456);if (s1 s2) {cout 等于 endl;}elsecout 不等于 endl;if (s1 ! s2) {cout bu等于 endl;}elsecout 等于 endl;
}
/*
不等于
bu等于
456执行了析构函数
123执行了析构函数
*/
int main() {test01();//test02();//test03();//test04();//test05();return 0;
}