以下是我自己编写的一个字符串类MyStr,不知这样写是否有不妥之处,求大侠们给指点,学习了!
#include "stdafx.h"
#include <iostream>
using namespace std;
class MyStr
{
friend ostream & operator<<(ostream & out,MyStr & mystr);
public:
MyStr(char *mystr=NULL):str(mystr){}
MyStr(const MyStr & mystr){str=mystr.str;}
MyStr& operator =(const MyStr &rhs)
{
if (this!=&rhs)
{
str=rhs.str;
}
return *this;
}
~MyStr(){}
private:
char * str;
};
ostream & operator<<(ostream & out,MyStr & mystr)
{
out<<mystr.str;
return out;
}
int _tmain(int argc, _TCHAR* argv[])
{
char *pstr="123456";
//MyStr defstr(pstr);
/*MyStr defstr;
defstr=pstr;*/
MyStr defstr("123");
cout<<defstr<<endl;
system("pause");
return 0;
}