实现一个string类时的问题
以下的运行过没有问题#include <iostream>
#include <cstring>
using namespace std;
class mystring
{private:
char *_str;
int _size;
public:
mystring(){_size=0;_str=NULL;}//缺省构造函数
mystring(const char *str);
mystring(const mystring & a);
friend ostream & operator <<(ostream &,mystring &);
friend istream & operator >>(istream & ,mystring &);
mystring & operator =(const char *);
mystring & operator =(const mystring &);
char operator [](int index);
~mystring(){delete _str;}
// int size(){char *p ,while()}
};
//string类构造函数
mystring::mystring(const char *str)
{if(!str)
{_size=0;_str=NULL;}
else
{_size=strlen(str);
_str=new char[_size+1];
strcpy(_str,str);}
}
mystring::mystring(const mystring &a)
{*this=a;}
//重载<<运算符
ostream & operator << (ostream &out,mystring &b)
{out<<b._str;
return out; }
//重载>>运算符
istream & operator >>(istream &in ,mystring &b)
{in>>b._str;return in;}
//重载赋值运算符
mystring & mystring::operator = (const char *str)
{_size=strlen(str);
_str=new char[_size+1];
strcpy(_str,str);
return *this;
}
mystring & mystring::operator =(const mystring &a)
{if(this!=&a)
{delete [] _str;
_size=a._size;
if(a._str!=0)
{_str=new char[_size+1];
strcpy(_str,a._str);}
else _str=0;
}
return *this;
}
//重载[]
char mystring::operator [](int index)
{if(index>_size-1)
return 0;
else return *(_str+index); }
int main()
{
return 0;
}
#include <cstring>
using namespace std;
class mystring
{private:
char *_str;
int _size;
public:
mystring(){_size=0;_str=NULL;}//缺省构造函数
mystring(const char *str);
mystring(const mystring & a);
friend ostream & operator <<(ostream &,mystring &);
friend istream & operator >>(istream & ,mystring &);
mystring & operator =(const char *);
mystring & operator =(const mystring &);
char operator [](int index);
~mystring(){delete _str;}
// int size(){char *p ,while()}
};
//string类构造函数
mystring::mystring(const char *str)
{if(!str)
{_size=0;_str=NULL;}
else
{_size=strlen(str);
_str=new char[_size+1];
strcpy(_str,str);}
}
mystring::mystring(const mystring &a)
{*this=a;}
//重载<<运算符
ostream & operator << (ostream &out,mystring &b)
{out<<b._str;
return out; }
//重载>>运算符
istream & operator >>(istream &in ,mystring &b)
{in>>b._str;return in;}
//重载赋值运算符
mystring & mystring::operator = (const char *str)
{_size=strlen(str);
_str=new char[_size+1];
strcpy(_str,str);
return *this;
}
mystring & mystring::operator =(const mystring &a)
{if(this!=&a)
{delete [] _str;
_size=a._size;
if(a._str!=0)
{_str=new char[_size+1];
strcpy(_str,a._str);}
else _str=0;
}
return *this;
}
//重载[]
char mystring::operator [](int index)
{if(index>_size-1)
return 0;
else return *(_str+index); }
int main()
{
return 0;
}
现在我有几个疑问,一直在困扰着我,请各位帮忙解决,请示都是运算符重载的问题
1.
//重载赋值运算符
mystring & mystring::operator = (const char *str)
{_size=strlen(str);
_str=new char[_size+1];
strcpy(_str,str);
return *this;
}
mystring & mystring::operator = (const char *str)
{_size=strlen(str);
_str=new char[_size+1];
strcpy(_str,str);
return *this;
}
这里,定义=运算符的返回是mystring的引用吗?为什么要这样。赋值的细节在函数体里面已经够了,最后返回个mystring的对象有什么用。用的时候时候没有需要用到返回值。
2.能否将=的重载改为普通函数,然后在mysring类中声明为友元函数