各種演算子のリロードを簡単に実現

3878 ワード

#define _CRT_SECURE_NO_WARNINGS
#include
#include
using namespace std;
class mystring
{ 
public:
     friend ostream& operator <>(istream& cin, mystring & p);  //   >>   
     mystring();//     
     mystring(char *a);//     
     mystring(const mystring &p);//    
     mystring& operator=(char *a);//=  ,            
     mystring& operator= (mystring &p);//=  ,          
     char& operator[](int a);//  []
     mystring operator+(mystring &p); //+     
     mystring operator+(char *a);
     ~mystring();//    
private:
     char *mystr;     //       
     int mysize;        //       
};
mystring::mystring(char *a) //         
{
     this->mystr = new char[strlen(a) + 1];
     strcpy(this->mystr, a);
     this->mysize = strlen(a);
}
mystring::mystring(const mystring &p)//      
{
     this->mystr = new char[strlen(p.mystr) + 1];
     strcpy(this->mystr, p.mystr);
     this->mysize = p.mysize;
}
mystring::~mystring() //    
{
     if (this->mystr != NULL)
     {
  	delete[] this->mystr;
 	 this->mystr = NULL;
     }
}
ostream& operator <>(istream& cin,mystring & p)
{
     if (p.mysize != NULL)     //          
     {
    	 delete[] p.mystr;
 	 p.mystr = NULL;
     }
     char buf[1024];              //              
     cin >> buf;                     //    
     p.mystr = new char[strlen(buf) + 1];  //        
     strcpy(p.mystr, buf);
     p.mysize = strlen(buf);
     return cin;
}
mystring& mystring::operator=(char *a)
{
     if (this->mystr != NULL)
     {
 	 delete[] this->mystr;
  	this->mystr = NULL;
     }
     this->mystr = new char[strlen(a) + 1];
     strcpy(this->mystr, a);
     this->mysize = strlen(a);
     return *this;
}
mystring& mystring::operator=(mystring &p)
{
     if (this->mystr != NULL)
     {
 	 delete[] this->mystr;
  	this->mystr = NULL;
     }
     this->mystr = new char[strlen(p.mystr) + 1];
     strcpy(this->mystr, p.mystr);
     this->mysize = p.mysize;
     return *this;
}
char& mystring::operator[](int a)
{
     return this->mystr[a];
}
mystring mystring::operator+(mystring &p)
{
     int newsize = this->mysize + p.mysize + 1;
     char *pp = new char[newsize];
     memset(pp, 0, newsize);
     strcat(pp, this->mystr);
     strcat(pp, p.mystr);
     mystring newstr(pp);
     delete[] pp;
     return newstr;
}
mystring mystring::operator+(char *a)
{
     int newsize = this->mysize + strlen(a) + 1;
     char *pp = new char[newsize];
     memset(pp, 0, newsize);
     strcat(pp, this->mystr);
     strcat(pp, a);
     mystring newstr(pp);
     delete[] pp;
     return newstr;
}
void test()                      //    1
{
     mystring p1("   ");
     mystring p2 = "lajio";
     cout << p2;
     cout << p1;
     cout << "      " << endl;
     cin >> p1;
     cout << p1;
} 
void test2()                           //    2 
{
     mystring p1("   ");
     mystring p2="csdlkcs3123";  //   mystring p2="csdlkcs3123";   mystring p2(csdlkcs3123),                  
     p2 = p1;                    //       , =    ,                ,                   
     p2 = "1321654chuaih";       //   p2="1321654chuaih"                         =       
     cout << p2;
     p2[0] = 'w';                       //         
     cout << p2[0] << endl;             //               
}
void test3()
{
     mystring p11("  ");
     mystring p22("      ");
     mystring p33=" ";  //       ,                
     p33 = p22 + p11;//       
     cout << p33;
}
int main()
{
     //test();
     //test2();
     test3();
     return 0;
}