#include <iostream>
#include<string>
using namespace std;
class mystring{
public:
mystring(const char *str = NULL);//普通构造函数
mystring(const mystring &other);//赋值
~mystring(void);
mystring& operator=(const mystring &other);
friend ostream& operator<<(ostream& out,mystring &str);
friend istream& operator>>(istream& in,mystring &str);
char *m_str;
};
mystring::mystring(const char *str){
if(str==NULL){
m_str=new char[1];
*m_str='\0';
}else{
int len = strlen(str);
m_str=new char[len];
strcpy(m_str,str);
}
}
mystring::mystring(const mystring &other){
m_str=other.m_str;
}
mystring::~mystring(){
delete[] m_str;
}
mystring& mystring::operator=(const mystring &other){
m_str=other.m_str;
return *this;
}
ostream& operator<<(ostream& out,mystring &str){
out<<str.m_str;
return out;
}
istream& operator>>(istream& in,mystring &str){
in>>str.m_str;
return in;
}
int main(){
mystring a;
cin>>a;
cout<<a<<endl;
return 0;
}
string类作为例子详解c++重载>>和<<
最新推荐文章于 2023-03-29 19:43:20 发布