#include<iostream>
#include<string.h>
using namespace std;
class KB
{
private:
char *s;//字符串
public:
KB(const char *p=0)
{
s=new char[strlen(p)+1];
strcpy(s,p);
}
friend KB &operator+=(KB &str1,KB &str2)
{
char *k=new char[strlen(str1.s)+strlen(str2.s)+1];
strcpy(k,str1.s);
strcat(k,str2.s);
str1.s=new char[strlen(k)+1];
strcpy(str1.s,k);
return str1;
}
void print()
{
cout<<s<<endl;
}
};
int main()
{
KB z1("abcd"),z2("1234");
z1+=z2;
z1.print();
}
c++连接字符串(重载运算符+=)
最新推荐文章于 2025-03-18 17:55:49 发布