64,637
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
#include <assert.h>
using namespace std;
class String
{
public:
String(int size=DEFAULTSIZE)
{
m_data=new char[size+1];
}
String (char *strSrc)
{
int size=strlen(strSrc);
assert(size>0);
m_data=new char[size+1];
strcpy(m_data,strSrc);
}
String(const String &s)
{
if (this==&s)
{
return ;
}
m_data=new char[strlen(s.m_data)+1];
strcpy(m_data,s.m_data);
}
~String()
{
delete []m_data;
}
void Print(void)
{
//delete m_data;
for (int i=0;i <DEFAULTSIZE;i++)
{
printf("%d\n",m_data[i]);
}
cout <<"-----------------------------------------" <<endl;
cout <<m_data <<endl;
}
String& operator = (String &s)
{
if (this==&s)
{
return *this;
}
delete []m_data;
m_data=new char[strlen(s.m_data)+1];
strcpy(m_data,s.m_data);
return *this;
}
String operator+ (const String& str1);
private:
char *m_data;
static const int DEFAULTSIZE;
};
const int String::DEFAULTSIZE = 10;
String String::operator+ (const String& str2)
{
String temp(strlen(m_data)+strlen(str2.m_data)+1);
strcpy(temp.m_data,m_data);
strcat(temp.m_data,str2.m_data);
return temp;
}
int main()
{
String a("12345678");
a.Print();
String b;
b=a;
b.Print();
String c;
c=(a+b); //這一句有問題,是什么原因?
c.Print();
system("pause");
return 0;
}
//改成这样
#include <iostream>
#include <assert.h>
using namespace std;
class String
{
public:
String(int size=DEFAULTSIZE)
{
m_data=new char[size+1];
}
String (char *strSrc)
{
int size=strlen(strSrc);
assert(size>0);
m_data=new char[size+1];
strcpy(m_data,strSrc);
}
~String()
{
delete []m_data;
}
void Print(void)
{
//delete m_data;
for (int i=0;i <DEFAULTSIZE;i++)
{
printf("%d\n",m_data[i]);
}
cout <<"-----------------------------------------" <<endl;
cout <<m_data <<endl;
}
String& operator = (String &s)
{
if (this==&s)
{
return *this;
}
delete []m_data;
m_data=new char[strlen(s.m_data)+1];
strcpy(m_data,s.m_data);
return *this;
}
String& operator+ (const String& str1)
{
strcat(this->m_data,str1.m_data);
return *this;
}
private:
char *m_data;
static int DEFAULTSIZE;
};
int String::DEFAULTSIZE=0;
int main()
{
String a("12345678");
a.Print();
String b;
b=a;
b.Print();
String c;
c=(a+b); //這一句有問題,是什么原因?
c.Print();
system("pause");
return 0;
}
#include <iostream>
#include <assert.h>
using namespace std;
class String
{
public:
String(int size=DEFAULTSIZE)
{
m_data=new char[size+1];
}
String (char *strSrc)
{
int size=strlen(strSrc);
assert(size>0);
m_data=new char[size+1];
strcpy(m_data,strSrc);
}
~String()
{
delete []m_data;
}
void Print(void)
{
//delete m_data;
for (int i=0;i <DEFAULTSIZE;i++)
{
printf("%d\n",m_data[i]);
}
cout <<"-----------------------------------------" <<endl;
cout <<m_data <<endl;
}
String& operator = (String &s)
{
if (this==&s)
{
return *this;
}
delete []m_data;
m_data=new char[strlen(s.m_data)+1];
strcpy(m_data,s.m_data);
return *this;
}
friend String& operator+ (const String& str1,const String& str2); //用返回引用
private:
char *m_data;
static int DEFAULTSIZE;
};
int String::DEFAULTSIZE=10;
String& operator+ (const String& str1,const String& str2)
{
String temp(strlen(str1.m_data)+strlen(str2.m_data)+1);
strcpy(temp.m_data,str1.m_data);
strcpy(temp.m_data,str2.m_data);
return temp;
}
int main()
{
String a("12345678");
a.Print();
String b;
b=a;
b.Print();
String c;
c=(a+b); //這一句有問題,是什么原因?
c.Print();
system("pause");
return 0;
}
#include <iostream>
using namespace std;
class String
{
public:
String(int size=DEFAULTSIZE)
{
m_data=new char[size+1];
}
String (char *strSrc)
{
int size=strlen(strSrc);
// assert(size>0);
m_data=new char[size+1];
strcpy(m_data,strSrc);
}
~String()
{
delete []m_data;
}
void Print(void)
{
//delete m_data;
for (int i=0;i <DEFAULTSIZE;i++)
{
printf("%d\n",m_data[i]);
}
cout <<"-----------------------------------------" <<endl;
cout <<m_data <<endl;
}
String(const String &s) //编译器自动生产的拷贝构造函数,只是位拷贝,这样m_data只是保存地址,即temp.m_data指向的位置
//temp离开作用域后,temp.m_data就释放了。要拷贝m_data指向的内容,需要自己实现
{
m_data=new char[strlen(s.m_data)+1];
strcpy(m_data,s.m_data);
}
String& operator = (String &s)
{
if (this==&s)
{
return *this;
}
delete []m_data;
m_data=new char[strlen(s.m_data)+1];
strcpy(m_data,s.m_data);
return *this;
}
friend String operator+ (const String& str1,const String& str2);
private:
char *m_data;
static const int DEFAULTSIZE=10;
};
String operator+ (const String& str1,const String& str2)
{
String temp(strlen(str1.m_data)+strlen(str2.m_data)+1);
strcpy(temp.m_data,str1.m_data);
strcpy(temp.m_data,str2.m_data);
return temp; //需要拷贝构造函数
}
int main()
{
String a("12345678");
a.Print();
String b;
b=a;
b.Print();
String c;
c=(a+b); //要用到拷贝构造函数
c.Print();
system("pause");
return 0;
}
//String& operator = (String &s)