注册 登录
编程论坛 C++ Builder

####请高手帮忙看看###

wycstc100 发布于 2010-05-23 14:33, 852 次点击
#include "stdafx.h"
#include "iostream"
#include "string"
using namespace std;
class string1
{ private:  int len;
            char *p;
public: string1(char *q ) {len=strlen(q)+1;p=new char(len);strcpy(p,q);}
        string1(){}
        string1(string1 &s) { len=s.len; p=new char(s.len);strcpy(p,s.p);}
        string1 operator =(string1 &str);
        string1 operator +(string1 str);
        string1 operator + (char* str);
        
        void display() {cout<<p<<endl;}
};
string1 string1::operator + (string1 str)
{  len=str.len;
    strcat(p,str.p);
     return *this;}
string1 string1::operator + (char* str)
{   
    strcat(p,str);
     return *this;}
string1 string1::operator =(string1 &str)
{if(this!=&str)
{ len=str.len ;
   delete []p;
   p=new char[len+1];
    strcpy(p,str.p);
}
return *this;
}
int main(int argc, char* argv[])
{   
    string1 str1("abc"),str2("edf"),str3,str4,str5;
   
    str3=str1+"sdsd";
    str4=str1+str2;
    str5=str1;
     str3.display();
    str4.display();
    str5.display();
    return 0;
}


在delete【】p;删除后就可以这是什么原因
0 回复
1